HTML: submit the form to the radio button

I have a form that displays two radio buttons (using PHP).

echo "<form action=\"next.php\" method=\"post\">"; echo "<input type=\"radio\" name=\"paid\" value=\"0\" checked=\"checked\">No<br>"; echo "<input type=\"radio\" name=\"paid\" value=\"1\">Yes<br>"; echo "</form>"; 

By default, one of the switches is used. If the user checks another switch, I would like the form to submit itself (without having to click the submit button).

+6
source share
3 answers

You can do this with jQuery (put the code inside the </body> ):

 <script type='text/javascript'> $(document).ready(function() { $('input[name=paid]').change(function(){ $('form').submit(); }); }); </script> 

DEMO HERE

To add jQuery to your page, put this line in the <head> :

 <script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js'> </script> 
+13
source

you can add this to your input:

 onchange='this.form.submit();' 

as:

 echo "<form action=\"next.php\" method=\"post\">"; echo "<input onchange='this.form.submit();' type=\"radio\" name=\"paid\" value=\"0\" checked=\"checked\">No<br>"; echo "<input onchange='this.form.submit();' type=\"radio\" name=\"paid\" value=\"1\">Yes<br>"; echo "</form>"; 

Demo

+18
source

Use the onchange listener:

 $("#paid").change(function(){ alert("test"); }); 

You just need to give your radio book an identifier in order to go with the name. The identifier can be the same as the name, so that it is simple for you.

+2
source

Source: https://habr.com/ru/post/952947/


All Articles