1 Replies - 398 Views - Last Post: 14 April 2009 - 05:27 AM

#1 the1corrupted  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 165
  • Joined: 31-March 09

Radio and Checkbox values inappropriately appearing

Posted 13 April 2009 - 01:49 PM

My last topic should be deleted, because now I know what my REAL problem is...

I have a form with a series of radio buttons and checkboxes, but when I submit the form blank, Javascript says they have a value!

So if someone could help me figure out why that even though the radio buttons are blank, they seem to have a default value?

If you really want, I can post the code, but be very warned, the code is huge.

But, in short, this is what I have.

<script type="text/javascript" language="javascript">
if (document.layers) {
	browserType = "nn4"
} else if (document.all) {
	browserType = "ie"
} else {
	browserType= "gecko"
}

function safeGetElement(id) {
	if (browserType == "gecko" )
		return eval("document.getElementById('" + id + "')");
	else if (browserType == "ie")
		return eval("document.getElementById('" + id + "')");
	else 
		return eval("document.layers['" + id + "']");
}

function FormCheck () {
	var question = 2;
	var quid = "q" + question;
	
	while (question<=13) {
		var quid = "q" + question;
		
		var QVal = safeGetElement(quid).value;
		
		var tracker = tracker + "\n" + quid + " " + QVal;
		
		question++;
	}
	
	alert('Check the form! ' + tracker);
}
</script>
</head>
<body>
Question 1: Who are you?<br>
<input name="who" type="radio" value="1" id="q2" /> I am Me 
<input name="who" type="radio" value="2" id="q2" /> I am You  



Just repeat that one question about 13 times over and basically, this is what it outputs in the alert...

"Check the form!
q2 1
q3 1
etc..."
(Those intergers next to q2 are the values)
Now, it also appears that no matter how I change it, these values remain constant.

Is This A Good Question/Topic? 0
  • +

Replies To: Radio and Checkbox values inappropriately appearing

#2 thehat  Icon User is offline

  • awake ? web();
  • member icon

Reputation: 106
  • View blog
  • Posts: 951
  • Joined: 28-February 08

Re: Radio and Checkbox values inappropriately appearing

Posted 14 April 2009 - 05:27 AM

You know both your radio buttons have the same id, right? An element's id should be unique.

Radio buttons always have a value, so if you want to see if an individual radio button has been checked you can look at it's checked attribute. If you want to know if one button in a group has been checked, look at the value of the group, not the individual button.

<form name="myForm">
   <input name="who" type="radio" value="1" id="q2" /> I am Me
   <input name="who" type="radio" value="2" id="q2" /> I am You
</form>
<script type="text/javascript">
   alert(document.forms['myForm'].who.value);
</script>



The javascript should alert 'undefined' because neither radio button is checked.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1