简体   繁体   English

使用JavaScript手动提交表单不会发送提交按钮

[英]Manually submitting a form using JavaScript doesn't send the submit button

I have a form that has two submit buttons. 我有一个有两个提交按钮的表单。 I want to submit the form manually using JavaScript and have the input button used to submit the form posted along with the other form elements, as it would be if the form was submitted automatically. 我想使用JavaScript手动提交表单,并使用输入按钮提交与其他表单元素一起发布的表单,就像表单自动提交一样。 There's quite a lot of chatter on this subject, but I can't find an answer. 关于这个问题有很多喋喋不休,但我找不到答案。

<form method="post" action="echoToScreenAndLog.jsp" id="form1"> 
    <input id="field1" name="field1"/>
    <input type="text" size="20" id="field2" name="field2"/>

    <input type="submit" value="Do One" name="sub1_name" id="sub1_id"/>
    <input type="submit" value="Do Two" name="sub2_name" id="sub2_id"/>
</form> 

When the form is submitted above using the "Do One" button, the posted parameters are field1="xxx" , field2="yyy" , sub1_name="Do One" . 当使用“Do One”按钮在上面提交表单时,发布的参数是field1="xxx"field2="yyy"sub1_name="Do One"

But I want to submit the form manually... 但我想手动提交表格......

<form method="post" action="echoToScreenAndLog.jsp" id="form1"> 
    <input id="field1" name="field1"/>
    <input type="text" size="20" id="field2" name="field2"/>

    <input type="submit" value="Do One" name="sub1_name" id="sub1_id"/>
    <input type="submit" value="Do Two" name="sub2_name" id="sub2_id"/>
</form> 
<script type="text/javascript">
    var btn = document.getElementById('sub1_id');
    btn.onclick=function() {
        return mySubmit(document.getElementById('form1'), ...);
    }
</script>

but doing a manual submission of the form in the mySubmit function does not post the sub1_name parameter. 但是在mySubmit函数中手动提交表单不会发布sub1_name参数。 I can understand that - I've bypassed the submission so the form is not being submitted using the buttons and therefore it makes no sense to post a parameter representing the button used to submit the form. 我可以理解 - 我绕过了提交,因此表单没有使用按钮提交,因此发布表示用于提交表单的按钮的参数是没有意义的。

When I look at the elements of the form in the onclick handler, I can see both buttons. 当我在onclick处理程序中查看表单的元素时,我可以看到两个按钮。 I'm not overly surprised by that either, they are elements on the form after all, but what I don't get is that if I add an element inside my onclick handler then the element I add IS posted and the two original submit buttons are not posted. 我也不会对它过于惊讶,毕竟它们是表单上的元素,但我没有得到的是,如果我在onclick处理程序中添加一个元素,那么我添加的元素是IS发布的,两个原始的提交按钮没有发布。 Just to complete the picture, here's the code that adds the element: 只是为了完成图片,这里是添加元素的代码:

<script type="text/javascript">
    var btn = document.getElementById('sub1_id');
    btn.onclick=function() {
        var f = document.getElementById('form1');
        var s = document.createElement("input");
        s.type="hidden"; s.name="xsubmit_name"; s.value="Bob"; s.id="xsubmit_id";            
        f.appendChild(s);

        // s gets posted
        return mySubmit(f, ...);
    }
</script>

Adding the input element could work for me, but I'm confused how the browser knows to post my added element and not the original two input elements. 添加输入元素可能对我有用,但我很困惑浏览器如何知道发布我添加的元素而不是原始的两个输入元素。

Thank you. 谢谢。

The specification says that the first step for form submission is: 规范说表单提交的第一步是:

Step one: Identify the successful controls 第一步:确定成功的控制

"Successful controls" are defined as: “成功控制”定义为:

A successful control is "valid" for submission. 成功的控制对于提交是“有效的”。 Every successful control has its control name paired with its current value as part of the submitted form data set. 每个成功的控件都将其控件名称与其当前值配对,作为提交的表单数据集的一部分。 A successful control must be defined within a FORM element and must have a control name. 必须在FORM元素中定义成功的控件,并且必须具有控件名称。

However: 然而:

... ...

  • If a form contains more than one submit button, only the activated submit button is successful. 如果表单包含多个提交按钮,则只有激活的提交按钮成功。

Since none of the submit buttons are activated, none are sent. 由于没有激活任何提交按钮,因此不会发送任何按钮。 Hidden input elements, on the other hand, are valid and will just be submitted along. 另一方面,隐藏的输入元素是有效的,只是一起提交。 Note that you add the hidden elements before calling mySubmit() , so at the time the above steps are executed (ie during submit), the hidden element is just another successful control part of the form, and thus sent. 请注意, 调用mySubmit() 之前添加隐藏元素,因此在执行上述步骤时(即在提交期间),隐藏元素只是表单的另一个成功控制部分,因此被发送。

may use 可能会用

var btn = document.getElementById('sub1_id');
btn.onsubmit=function() {
   return false;
}

btn.onclick=function() {
    var f = document.getElementById('form1');
    var s = document.createElement("input");
    s.type="hidden"; s.name="xsubmit_name"; s.value="Bob"; s.id="xsubmit_id";            
    f.appendChild(s);

    f.submit()
}

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

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