提交表单而不使用提交按钮 [英] Submit a form without using submit button
问题描述
我正在使用表单提交数据以从数据库中获取记录.
在表单中,我使用了两个选择标记选项.
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.
推荐答案
使一个函数检查您想要的所有内容是否已设置,然后,如果有,请提交以下表单:
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();
}
}
然后根据您的选择,绑定 onchange 事件以运行该功能.
Then on your select, bind the onchange event to run the function.
这是一个有效的示例: http://jsfiddle.net/JE6AM/
Here is a working example: 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!');
}
}
我已经用alert()替换了提交,向您展示了代码是如何触发的.
I've replaced the submit with an alert() to show you how the code triggers.
您可以使用$_REQUEST['selectCar']
访问该值.
这篇关于提交表单而不使用提交按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!