Submit a form without using submit button

2020-06-25 04:53发布

问题:

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.

回答1:

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 onchange event to run the function.

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:

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.

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



回答2:

Every form has a submit() function.

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

can be submitted with

document.myForm.submit();


回答3:

Jquery has been used to easily to submit the form.

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

</select>
</form>

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


回答4:

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