简体   繁体   中英

radio button form without submit

Lets say I have 5 radio buttons like below. How would I submit this form without a submit button when all radio buttons are checked. once submitted I would like a response to be posted under the form. For example if all radio buttons are yes it would say "all radio buttons are yes" if all radio buttons are no "all radio buttons are no" and if theres a mixed "radio buttons consist of a mix of yes and no". Really struggling to find anything on this maybe I'm searching the wrong thing as I'm pretty sure I've seen things like this on a few websites. Thanks for your help :).

 <input type="radio" name="q1" value="yes">Yes <input type="radio" name="q1" value="no">no<br> <input type="radio" name="q2" value="yes">Yes <input type="radio" name="q2" value="no">no<br> <input type="radio" name="q3" value="yes">Yes <input type="radio" name="q3" value="no">no<br> <input type="radio" name="q4" value="yes">Yes <input type="radio" name="q4" value="no">no<br> <input type="radio" name="q5" value="yes">Yes <input type="radio" name="q5" value="no">no 

You can use a JavaSript function to achieve this. Whenever a radio button is checked or unchecked, you can call this function. So you can change

<input type="radio" name="q5" value="yes">Yes
<input type="radio" name="q5" value="no">no

To:

<input type="radio" name="q5" value="yes" onClick="validateRadio();">Yes
<input type="radio" name="q5" value="no" onClick="validateRadio();">no

inside the validateRadio() you can use an if condition to check if all "Yes" are checked, all "No" are checked or if it is a combination of Yes and No.

And you can use the following line to submit the form.

document.getElementById("myForm").submit();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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