简体   繁体   English

表单onsubmit忽略动作

[英]form onsubmit ignores action

I am new to JavaScript. 我是JavaScript新手。 I have a form with an action and I need to fill the form inputs using JavaScript on submitting. 我有一个带有动作的表单,我需要在提交时使用JavaScript填写表单输入。 I use an onsubmit function which executes as expected, but the form didn't hit the action in the action attribute and no data is sent to the server. 我使用了一个onsubmit函数,该函数按预期执行,但是表单没有在action属性中找到该action并且没有数据发送到服务器。
Can anybody help me find what is missing ? 有人可以帮助我找到缺失的地方吗?

JavaScript/jQuery: JavaScript / jQuery:

$("#myForm").submit(function (e) {
  e.preventDefault();
  return fillFormValues();
})

function fillFormValues()
{
  $.get( ...,function( data ) {
    //fill my inputs values here
    return true;
  });
  return false;
}

HTML: HTML:

<form action="..." method="post" target="output_frame" id="myForm">
    <input name="first" id="first" type="hidden" value="">
    ...
    <input type="submit"  value="next">
</form>

what you can do is prevent the jQuery event default that would submit the form, do the ajax and once values are set trigger the native submit event that will bypass the jQuery submit listener 您可以做的是防止jQuery事件默认值提交表单,执行ajax,一旦设置了值,就触发本机提交事件,该事件将绕过jQuery提交侦听器

$("#myForm").submit(function(e) {
  e.preventDefault();
  fillFormValues(this);
});

function fillFormValues(form) {
  $.get(..., function(data) {
    //fill your inputs values here
    form.submit(); //trigger native submit which is not caught by jQuery
  });
}

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

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