简体   繁体   English

在使用javascript或jquery提交表单时需要帮助

[英]Need help with a form submission using javascript or jquery

I have a form which looks like this 我有一个看起来像这样的表格

@using (Html.BeginForm("ActionMethod","MyController FormMethod.Post)) {
 <button type="submit" value="Generate" name="action" class="button" id="btnGenerate">Generate Form</button>
 <button type="submit" value="Confirm" name="action" class="button" id="btnConfirm">Confirm</button>
}

and my javascript looks like this 和我的JavaScript看起来像这样

 <script type="text/javascript">
     $(function () {

         var genOverlay = jQuery('<div id="overlay"><div class="innerblock box-shadow"><p>Please remember to print the registration form and sign both copies.</p><div><a id="btnClose" href="#" class="button">Close</a></div></div>');

         var confirmOverlay = jQuery('<div id="overlay"><div class="innerblock box-shadow"><p>Changes can not be made to this record once it has been confirmed. Are you sure you would like to confirm this form?</p><div> <a id="btnConfirmConfirmation" href="#" class="button">Confirm</a> <a id="btnCancel" href="#" class="button button-red">Cancel</a></div></div>');

         $('#btnGenerate').click(function () {
             genOverlay.appendTo(document.body);
             return false;
         });

         $('#btnConfirm').click(function () {
             confirmOverlay.appendTo(document.body);
             return false;
         });


         $('#btnConfirmConfirmation').live('click', function () {
             // Need help on submitting the form with the input button value of btnConfirm.

             // $('#btnConfirm').submit(); does not work
             // return true;
         });

         $('#btnClose').live('click', function () {
             genOverlay.remove();
         });

         $('#btnCancel').live('click', function () {
             confirmOverlay.remove();
         });
     });
</script>

How would i go about implementing btnConfirmConfirmation click on the overlay to just submit the form normally with the action value of "Confirm"? 我将如何实施btnConfirmConfirmation,请点击叠加层,以通常的操作值“ Confirm”提交表单?

Thanks for any help 谢谢你的帮助

The .submit() method only applies to <form> elements. .submit()方法仅适用于<form>元素。 You could add an id to your form: 您可以在表单中添加一个ID:

<form id="myForm" ...>

Which, as you're using HtmlHelper to create it would be achieved with: 当您使用HtmlHelper创建它时,可以通过以下方法实现:

@using (Html.BeginForm("ActionMethod","MyController", FormMethod.Post, new { id = "myForm" })) { ...

And then call the submit method (documented here ) on that: 然后在其上调用Submit方法( 在此处记录 ):

$('#btnConfirmConfirmation').live('click', function () {
    $('#myForm').submit();
});

Or you could go to the form for your button by finding the closest ancestor form element for the button: 或者,您可以通过找到按钮最接近的祖先表单元素来转到按钮的表单:

$('#btnConfirmConfirmation').live('click', function () {
    $(this).closest('form').submit();
});

closest method is documented here . closest方法记录在这里

submit is an event handler of the form element. submit是form元素的事件处理程序。 This should work: 这应该工作:

$('#btnConfirm')[0].form.submit()
//All form elements have a property called "form" which refers to the parent form

If you've attached an identifier to your form, use this: 如果您在表单上附加了标识符,请使用以下代码:

$('#formId').submit();               //<form id="formId" ...
$('form[name="formName"]').submit(); //<form name="formName" ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM