Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Radio button onChange not working when trying to submit form

Posted on 2010-11-19
8
Medium Priority
?
912 Views
Last Modified: 2012-05-10
Hi,

The scenerio is as follows:

The form is loaded with radio buttons prepopulated.
I need to the form to submit if the user selects a new option.

Example code:

<form action="submit.php" method="post">
<input type="radio" name="mybutton" value="0" onChange="this.form.submit();" />
<input type="radio" name="mybutton" value="1" onChange="this.form.submit();" />
<input type="radio" name="mybutton" value="2" onChange="this.form.submit();" />
</form>

What have I done wrong?

Thanks
0
Comment
Question by:jagku
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 13

Expert Comment

by:dsmile
ID: 34174811
Use onclick instead

<form action="submit.php" method="post">
<input type="radio" name="mybutton" value="0" onclick="this.form.submit();" />
<input type="radio" name="mybutton" value="1" onclick="this.form.submit();" />
<input type="radio" name="mybutton" value="2" onclick="this.form.submit();" />
</form>
0
 
LVL 53

Expert Comment

by:Ryan Chong
ID: 34174813
try use:

onClick="this.form.submit();"

instead?
0
 

Author Comment

by:jagku
ID: 34187524
Hi Guys,

I did try using onClick (actually, I should have mentioned that - sorry!).
However, the problem was that this was firing everytime the user clicked the radio button (even if it was already checked).

Thanks
Dipen
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 53

Expert Comment

by:Ryan Chong
ID: 34187668
Try:

<form action="submit.php" method="post">
<input type="radio" name="mybutton" value="0" onClick="if ((!this.form.mybutton[0].checked)&&(!this.form.mybutton[1].checked)&&(!this.form.mybutton[1].checked)) this.form.submit();" />
<input type="radio" name="mybutton" value="1" onClick="if ((!this.form.mybutton[0].checked)&&(!this.form.mybutton[1].checked)&&(!this.form.mybutton[1].checked)) this.form.submit();" />
<input type="radio" name="mybutton" value="2" onClick="if ((!this.form.mybutton[0].checked)&&(!this.form.mybutton[1].checked)&&(!this.form.mybutton[1].checked)) this.form.submit();" />
</form>
0
 
LVL 53

Expert Comment

by:Ryan Chong
ID: 34187672
Try:

<form action="submit.php" method="post">
<input type="radio" name="mybutton" value="0" onClick="if ((!this.form.mybutton[0].checked)&&(!this.form.mybutton[1].checked)&&(!this.form.mybutton[2].checked)) this.form.submit();" />
<input type="radio" name="mybutton" value="1" onClick="if ((!this.form.mybutton[0].checked)&&(!this.form.mybutton[1].checked)&&(!this.form.mybutton[2].checked)) this.form.submit();" />
<input type="radio" name="mybutton" value="2" onClick="if ((!this.form.mybutton[0].checked)&&(!this.form.mybutton[1].checked)&&(!this.form.mybutton[2].checked)) this.form.submit();" />
</form>
0
 

Author Comment

by:jagku
ID: 34187835
Hi Ryancys,

Will the form submit if value = 0 is checked when we reach this screen and click on any of the buttons?

What I need (and I am starting to think that it isn't possible as the onClick event appears to fire after the radio button is changed (not before)) is to make sure that the form isn't submitted if (and only if) the radio button hasn't been changed.  I only want it to submit if there has been a radio button change.

0
 
LVL 53

Accepted Solution

by:
Ryan Chong earned 1500 total points
ID: 34188229
you can try something like this instead:

<form action="submit.php" method="post">
<input type="radio" name="mybutton" value="0" onClick="if (x!=0) {this.form.submit();} else {x=0;}" />
<input type="radio" name="mybutton" value="1" onClick="if (x!=1) {this.form.submit();} else {x=1;}" />
<input type="radio" name="mybutton" value="2" onClick="if (x!=2) {this.form.submit();} else {x=2;}" />
</form>
<script language="javascript">
      var frm = document.forms[0];
      var x = -1;
      if (frm.mybutton[0].checked) x = 0;
      if (frm.mybutton[1].checked) x = 1;
      if (frm.mybutton[2].checked) x = 2;
</script>
0
 

Author Closing Comment

by:jagku
ID: 34189618
thanks
0

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question