需要有关使用javascript或jquery的表单提交的帮助

时间:2011-09-20 15:32:40

标签: javascript jquery

我有一个看起来像这样的表格

@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>
}

我的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>

我如何实施btnConfirmConfirmation点击叠加层以正常提交表格,其动作值为“确认”?

感谢您的帮助

2 个答案:

答案 0 :(得分:2)

.submit()方法仅适用于<form>元素。您可以在表单中添加ID:

<form id="myForm" ...>

当您使用HtmlHelper创建它时,将通过以下方式实现:

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

然后在其上调用submit方法(记录为here):

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

或者您可以通过查找按钮的最近祖先表单元素来转到按钮的表单:

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

closest方法here

答案 1 :(得分:1)

submit是表单元素的事件处理程序。这应该有效:

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

如果您已在表单中附加标识符,请使用以下命令:

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