如何强制用户在javascript提示符中输入值?
我有这个jquery代码,它将打开一个提示框。但问题是用户是否在中输入值,如果用户单击“确定”,它仍会继续执行代码。是否仍有强制用户输入值,然后单击“确定”,否则他们无法单击“确定”按钮 这是我试过的,但没有成功如何强制用户在javascript提示符中输入值?,javascript,jquery,Javascript,Jquery,我有这个jquery代码,它将打开一个提示框。但问题是用户是否在中输入值,如果用户单击“确定”,它仍会继续执行代码。是否仍有强制用户输入值,然后单击“确定”,否则他们无法单击“确定”按钮 这是我试过的,但没有成功 $(function() { $(".adminapprovepost").click(function(){ var id = $(this).attr("id"); var tag = prompt("you must enter one keyword to approve th
$(function() {
$(".adminapprovepost").click(function(){
var id = $(this).attr("id");
var tag = prompt("you must enter one keyword to approve this post");
if(tag!=null)
{
$.ajax({
type: "POST",
url: "/admin/admin-approve-post.php",
data: {id:id,tag:tag},
success: function(){
}
});
$(this).parents(".postrecord").animate({ backgroundColor: "#fbc7c7" }, "fast")
.animate({ opacity: "hide" }, "slow");
}
return false;
});
});
您的测试是错误的,因为提示“我返回空格”类似于字符或空字符串,这实际上是在用户单击“确定”时发生的。我会写:
var tag = prompt("you must enter one keyword to approve this post");
if (tag != null) tag = tag.trim(); // assuming here you really want to do this
if (!tag) { // a non-empty value
:
您的测试是错误的,因为提示“我返回空格”类似于字符或空字符串,这实际上是在用户单击“确定”时发生的。我会写:
var tag = prompt("you must enter one keyword to approve this post");
if (tag != null) tag = tag.trim(); // assuming here you really want to do this
if (!tag) { // a non-empty value
:
它应该是这样工作的:
$(function() {
$(".adminapprovepost").click(function(){
var id = $(this).attr("id");
var proceed = true;
while(proceed) {
tag = prompt("you must enter one keyword to approve this post");
if (typeof(tag) == "string") {
tag = tag.trim()
if (tag!="")) {
proceed = false;
}
}
if (tag===null) {
proceed = false;
}
}
if(tag!=null)
{
$.ajax({
type: "POST",
url: "/admin/admin-approve-post.php",
data: {id:id,tag:tag},
success: function(){
}
});
$(this).parents(".postrecord").animate({ backgroundColor: "#fbc7c7" }, "fast")
.animate({ opacity: "hide" }, "slow");
}
return false;
});
});
编辑:现在用户还可以单击“取消”。它最终应该会起作用。我希望…它应该是这样工作的:
$(function() {
$(".adminapprovepost").click(function(){
var id = $(this).attr("id");
var proceed = true;
while(proceed) {
tag = prompt("you must enter one keyword to approve this post");
if (typeof(tag) == "string") {
tag = tag.trim()
if (tag!="")) {
proceed = false;
}
}
if (tag===null) {
proceed = false;
}
}
if(tag!=null)
{
$.ajax({
type: "POST",
url: "/admin/admin-approve-post.php",
data: {id:id,tag:tag},
success: function(){
}
});
$(this).parents(".postrecord").animate({ backgroundColor: "#fbc7c7" }, "fast")
.animate({ opacity: "hide" }, "slow");
}
return false;
});
});
编辑:现在用户还可以单击“取消”。它最终应该会起作用。我希望…您可以使用以下代码:
var proceed = false;
while(!proceed)
{
tag = prompt("Please input the string:");
if (typeof(tag) == "string")
{
tag = tag.trim();
if (tag=="")//// no value
{
proceed = false;
alert('Invalid entry!!!');
}
else
{
proceed=true;
}
}
if (tag===null) ////cancel hits
{
proceed = false;
alert('Approve process cancelled !!!');
break;
}
}
if(proceed)
{
/////put your code here
}
while循环将继续要求您输入字符串,除非您点击提示符中的取消按钮或输入有效条目。您可以使用以下代码:
var proceed = false;
while(!proceed)
{
tag = prompt("Please input the string:");
if (typeof(tag) == "string")
{
tag = tag.trim();
if (tag=="")//// no value
{
proceed = false;
alert('Invalid entry!!!');
}
else
{
proceed=true;
}
}
if (tag===null) ////cancel hits
{
proceed = false;
alert('Approve process cancelled !!!');
break;
}
}
if(proceed)
{
/////put your code here
}
while循环将继续要求您输入字符串,除非您点击提示符中的取消按钮或输入有效条目。var tag=null;虽然tag{tag=prompt….trim;}使用jQuery UI模式对话框,而不是使用此对话框,只有在用户不想使用staytag=prompt….trim时,才能在Chrome中停止该对话框;当用户单击“取消”时不工作,因为将返回null。并且不能修剪null。var tag=null;虽然tag{tag=prompt….trim;}使用jQuery UI模式对话框,而不是使用此对话框,只有在用户不想使用staytag=prompt….trim时,才能在Chrome中停止该对话框;当用户单击“取消”时不工作,因为将返回null。和null不能被修剪。问题是我不能单击“取消”按钮退出脚本?如果我单击“取消”,我会得到以下错误:TypeError:prompt。。。是空的。如何防止此问题?已更改,但仍出现此错误:TypeError:prompt。。。是空的。。。当我单击“取消”时,我将我的代码编辑为您的最新代码,但当我单击“取消”时,仍然捕获到以下错误:TypeError:prompt。。。是空的…不客气。谢谢你的耐心。我连续犯了太多简单的错误。我今天应该停止工作…问题是我不能单击“取消”按钮退出脚本?如果我单击“取消”,我会得到以下错误:TypeError:prompt。。。是空的。如何防止此问题?已更改,但仍出现此错误:TypeError:prompt。。。是空的。。。当我单击“取消”时,我将我的代码编辑为您的最新代码,但当我单击“取消”时,仍然捕获到以下错误:TypeError:prompt。。。是空的…不客气。谢谢你的耐心。我连续犯了太多简单的错误。我应该停止今天的工作…while循环将继续要求您输入字符串,除非您在提示符中单击“取消”按钮或输入有效条目。while循环将继续要求您输入字符串,除非您在提示符中单击“取消”按钮或输入有效条目