【问题标题】:Get dialog box value to determine submit the form or not获取对话框值以确定是否提交表单
【发布时间】:2013-09-04 06:31:48
【问题描述】:

我有这样的表格:

<form action="confirm-and-send.php" method="post" onsubmit="return(confirmation());">
    <input a lot of input fields here>
    <button type="submit" name="confirm_and_send" id="submit-button"  class="sent-to-client-bttn" style="margin-left:630px;margin-top: -125px;"></button>
</form> 

然后我有 jquery ui 功能

function confirmation(){
   $("#some-div").dialog({
        bgiframe: true,
        autoOpen: false,
        minHeight: 200,
        width: 350,
        modal: true,
        closeOnEscape: false,
        draggable: false,
        resizable: false,
        buttons: {
                'Yes': function(){
                    $(this).dialog('close');
                    callback(true);
                },
                'No': function(){
                    $(this).dialog('close');
                    callback(false);
                }
            }
    });
}
function callback(value){
     //do something
}

我正在关注他们所说的 here 和 here,但这对我不起作用。如果我把event.preventDefault() 是按钮将不会提交;如果我不把它提交表格,然后我选择两个选项中的任何一个。谁能给我一个合适的例子?

【问题讨论】:

    标签: javascript forms jquery-ui return-value


    【解决方案1】:

    将按钮移到表单之外。

    <form action="" method="post" id="my_form" onsubmit="">
            <input type="text" name="x"/>
    
        </form> 
        <button name="confirm_and_send" id="submit-button" value="Submit" class="sent-to-client-bttn" onclick="confirmation();">Submit</button>
        <div id="some-div" style="display:none">Hi</div>
    
    
    function confirmation(){
    
    
       $("#some-div").dialog({
            bgiframe: true,
            minHeight: 200,
            width: 350,
            modal: true,
            closeOnEscape: false,
            draggable: false,
            resizable: false,
            buttons: {
                    'Yes': function(){
                        $(this).dialog('close');
                        callback(true);
                    },
                    'No': function(){
                        $(this).dialog('close');
                        callback(false);
                    }
                }
        });
    
    
    }
    function callback(value){
         alert(value);
    }
    

    【讨论】:

    • 并删除autoOpen: false
    • 嘿,你的想法其实就是我需要的!将按钮从表单中取出。唯一剩下的是配置回调,我把 $('#myform').submit() 放在回调中,它解决了问题。感谢您的帮助
    • 抱歉回复晚了..!欢迎安德鲁..!!
    【解决方案2】:

    我认为您正在使用 JqueryUi 对话框.. 它不起作用.. 为此您必须使用 this

    我向你解释当你点击时会发生什么..

    它不会因为你的点击而停止..它会自动提交......所以你必须尝试默认..浏览器确认

    【讨论】:

    • AmitS 的回答已经到了重点。我的代码的问题不是对话框,而是我的表单结构。还是谢谢
    猜你喜欢
    • 2017-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-24
    • 2011-10-17
    • 2012-06-29
    • 2020-01-04
    • 1970-01-01
    相关资源
    最近更新 更多