在 JQuery 对话框中提交 HTML 表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5582593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:24:46  来源:igfitidea点击:

Submitting HTML form in JQuery dialog

jqueryhtmljquery-uijquery-ui-dialog

提问by Vonder

I am trying to convert an existing HTML form and make it appear in JQuery dialog. I am not sure how to change the code so the data is submitted after clicking the submit buton. My code is a follows:

我正在尝试转换现有的 HTML 表单并使其出现在 JQuery 对话框中。我不确定如何更改代码,以便在单击提交按钮后提交数据。我的代码如下:

Old form:

旧形式:

<form method="post" name="callbackrequest" id="ccallbackrequest" action="action.php" onsubmit="return validateThis(this)">

<button type="submit" class="submit">Submit Request</button>

New form:

新形式:

buttons: {
                "Submit": function() {
if ( bValid ) {

        HERE GOES THE CODE!

        $( this ).dialog( "close" );
              }

Then there is the new form:

然后是新的形式:

<form class="center" method="post" name="callbackrequest" id="ccallbackrequest" action="??">
 fields listed
no submit button
</form>

Any advice on how should I handle this?

关于我应该如何处理这个问题的任何建议?

回答by bpeterson76

Let's take it one step further...here's a dialog form submit with ajax!

让我们更进一步......这是一个使用ajax提交的对话框表单!

<form id="modalform" style="display:none">
     <input type="text" name="something">
     <input type="text" name="somethingelse">
</form>




$("#modalform").dialog({                                                            //Shows dialog
        height: 250,
        width: 450,
        modal: true,
        buttons: {
            "Cancel": function() {
                $( this ).dialog( "close" );
            },
            "Save": function() {
                $.ajax({
                    url: "/url/to/submit",                   //
                    timeout: 30000,
                    type: "POST",
                    data: $('#modalform').serialize(),
                    dataType: 'json',
                    error: function(XMLHttpRequest, textStatus, errorThrown)  {
                        alert("An error has occurred making the request: " + errorThrown)
                    },
                    success: function(data){                                                        
                         //Do stuff here on success such as modal info      
                             $( this ).dialog( "close" );
            }
        }
    });

回答by Xr.

Try $('#ccallbackrequest').submit();. The submit method just submits whichever form is returned by the selector.

试试$('#ccallbackrequest').submit();。submit 方法只提交选择器返回的任何表单。

回答by schizodactyl

You just need to put in

你只需要输入

 $("#FORM_ID").submit()