jQuery javascript未正确返回true/false
jQuery javascript not returning true/false properly
我编写这段代码是为了在提交表单之前显示提示。如果表单输入的值大于100,则会显示一个对话框,大致通知用户处理所需的时间。如果他们点击"确定",它应该返回true,并提交表格,如果他们点击取消,它应该回复false,并不提交表格。
问题是,表单没有等待这个响应,它无论如何都在提交。我不知道怎么了。。。
这是代码:
$(document).ready(function() {
$("form#generate_vouchers").submit(function(){
if($("input#quantity").val() > 100){
var warning = "It will take around " + Math.round(($("input#quantity").val() / 23)) + " seconds to generate this batch.<br />Generation will continue even if you leave this page.";
//Does around 23 codes per second, nearly all of that time is inserts.
$('<div title="Batch information"></div>').html(warning).dialog({
draggable: false,
modal: true,
minWidth: 350,
buttons: {
"Cancel" : function() {
$(this).dialog("close");
return false;
},
"Yes": function() {
$("input#submit").hide(300, function(){
$("img#loader").show(300);
});
return true;
}
}
});
}
else{
$("input#submit").hide(300, function(){
$("img#loader").show(300);
});
}
});
});
显示对话不会停止执行并等待它关闭。提交功能继续,return true
和return false
不执行任何操作。
您需要做的是立即停止表单提交(使用下面的e.PreventDefault
),然后在Yes
回调中再次提交表单。
$("form#generate_vouchers").submit(function(e){
if($("input#quantity").val() > 100){
e.preventDefault(); // stop submit
var warning = "It will take around " + Math.round(($("input#quantity").val() / 23)) + " seconds to generate this batch.<br />Generation will continue even if you leave this page.";
//Does around 23 codes per second, nearly all of that time is inserts.
$('<div title="Batch information"></div>').html(warning).dialog({
draggable: false,
modal: true,
minWidth: 350,
buttons: {
"Cancel" : function() {
$(this).dialog("close");
},
"Yes": function() {
$("form#generate_vouchers")[0].submit(); // submit form manually
$("input#submit").hide(300, function(){
$("img#loader").show(300);
});
}
}
});
}
else{
$("input#submit").hide(300, function(){
$("img#loader").show(300);
});
}
});
示例-http://jsfiddle.net/infernalbadger/ajRr7/
相关文章:
- JavaScript-切换“;全部检查”;复选框true/false
- true==false以某种方式计算为true
- Javascript似乎在说true ==false
- JavaScript onkeydown, return true,false
- 从异步函数返回true/false以用于同步if()
- 函数返回undefined而不是true/false
- 为什么true/true是1,true/false是无穷大,false/true是0,false/false是NaN
- 检查对象数组是否包含布尔值true/false
- 函数:根据字符串中是否存在子字符串返回 True/False
- 如何将 ng-show 与控制器中返回 true/false 的函数一起使用
- 在input.checked=true/false _without_ jQuery上触发一个事件
- jQuery javascript未正确返回true/false
- 如何从嵌套的jquery回调函数中返回true/false
- 如何在骨干模型属性中设置布尔值true/false
- 如何使用angular或javascript在基于true/false返回的html中显示值
- 基于true/false变量显示/隐藏进度条
- Node.js在返回true/false时停止函数
- 是否可以将true/false值传递给ajax函数
- 根据Webix中的复选框值编辑true/false
- Meteor:设置车把变量true/false