2016-07-05 57 views
0

我在JQuery中使用submit函数做一些验证,如果验证是真的,它会提交表单。但是,我添加了“你确定”对话框。我可以从对话框中传回true或false,然后提交表单,因为我目前被困在一个循环中。JQuery对话框返回true或false来提交函数

JQuery的:

$(function(){ 
<!-- form validation --> 
$("#form1").submit(function() {  

    //validation stuff 

    //Disable the submit button 
    $('#SubmitButton').attr('disabled', false); 



    // Only if true is returned from dialog?? 
    return true; 
}) }); 

HTML

<div id="testdialog" title="Please Check"> 
<p>Are you sure></p>        
    <div style="height: 30px; width:100px;"onclick="$('#testdialog').dialog('close');return true;"> 
Continue 
    </div> 
    <div style="height: 30px; width: 100px;" onclick="$('#testdialog').dialog('close');return false';"> 
Redo selection 
    </div> 
</div> 
+0

对话框是异步的,你不能从它们返回任何东西。 – Barmar

+0

您需要使用对话框按钮的“onClick”回调来执行所需的操作。 – Barmar

回答

0

您将需要设置一个全局/会话变量,它会告诉你,如果用户已经确认与否,这样的事情:

var confirmed = false; 
var confirmSubmit = function() { 
    $('#testdialog').dialog('close'); 
    confirmed = true; 
    $("#form1").submit(); 
    return true; 
} 
$(function(){ 
    <!-- form validation --> 
    $("#form1").submit(function() {  

    //validation stuff 

    //Disable the submit button 
    $('#SubmitButton').attr('disabled', false); 

    if (!confirmed) { 
     // Trigger the notification 
    } 


    // Only if true is returned from dialog?? 
    return confirmed; 
}) }); 

和html:

<div id="testdialog" title="Please Check"> 
    <p>Are you sure></p>        
    <div style="height: 30px; width:100px;"onclick="confirmSubmit"> 
     Continue 
    </div> 
    <div style="height: 30px; width: 100px;" onclick="$('#testdialog').dialog('close');return false';"> 
     Redo selection 
    </div> 
</div> 
+0

感谢您的建议,它足够的问题排序。我有问题让onclick =“confirmSubmit”工作,并决定改用监听器。 – user2452357

1

感谢您的意见,它足够的问题排序出来。我有问题让onclick =“confirmSubmit”工作,并决定改用监听器。

$(function(){ 
    $('#btnconf').click(function() { 
    $('#testdialog').dialog('close'); 
    confirmed = true; 
    $("#form1").submit(); 
    return true; 
    }); 

在html中,我将id =“btnconf”添加到按钮中。

嘿presto !!作品一种享受!再次感谢!