javascript 不使用提交按钮提交表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17784936/
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-10-27 09:37:22  来源:igfitidea点击:

Submit a form without using submit button

javascriptforms

提问by bhai

I am using a form to submit the data to get the records from the database.
In the form I am using the two select tag options.

我正在使用表单提交数据以从数据库中获取记录。
在表单中,我使用了两个选择标签选项。

So after selecting the options the form should submit without using the submit button. I am waiting for the response to submit the form after selecting the inputs without using the submit button(or any button) it should submit automatically.

所以在选择选项后,表单应该提交而不使用提交按钮。我正在等待响应以在选择输入后提交表单而不使用它应该自动提交的提交按钮(或任何按钮)。

回答by MyPasswordIsLasercats

Every form has a submit()function.

每个表格都有一个submit()功能。

<form name="myForm">
...
</form>

can be submitted with

可以提交

document.myForm.submit();

回答by NibblyPig

Make a function to check everything you want has been set, and then if it has, submit the form:

制作一个函数来检查你想要的一切都已经设置,然后如果有,提交表单:

function submitIfFormComplete()
{
  // Check the select has something selected
  if (document.getElementById('selectOne').selectedIndex > 0)
  {
      document.getElementById('formID').submit();
  }
}

Then on your select, bind the onchangeevent to run the function.

然后在您的选择上,绑定onchange事件以运行该函数。

Here is a working example: http://jsfiddle.net/JE6AM/

这是一个工作示例:http: //jsfiddle.net/JE6AM/

Select your car make:
<select id='sel1' name='selectCar' onchange="checkAndSubmit()">
    <option value="0">Select...</option>
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select> 
<br/><br/>
Select your gender:
<select id='sel2' name='selectGender' onchange="checkAndSubmit()">
    <option value="0">Select...</option>
  <option value="Male">Volvo</option>
  <option value="Female">Saab</option>
</select> 

Javascript:

Javascript:

function checkAndSubmit()
{
  if (document.getElementById('sel1').selectedIndex > 0
     && document.getElementById('sel2').selectedIndex > 0)
  {
      //document.getElementById('formID').submit();
      alert('both have been selected!');
  }
}

I've replaced the submit with an alert() to show you how the code triggers.

我已将提交替换为 alert() 以向您展示代码如何触发。

Edit: You can use $_REQUEST['selectCar']to access the value.

编辑:您可以使用$_REQUEST['selectCar']来访问该值。

回答by sakthi.krr

Jquery has been used to easily to submit the form.

Jquery 已经被用来方便地提交表单。

<form id="test">
<select name="select1" id="select1">

</select>
</form>

$('#select1').change(function() {
    $("#test");
});

回答by Biswajit Paul

<form action="submit.php" id="submitform" method="post">
Name:<input type="text" name="nm"> <br>
Age:<input type="number" name="age">
<a href="javascript:document.getElementById('submitform').submit()">submit</a>