form name 取得 radio 的值

form name 取得 radio 的值

<form name="radioForm">
<input name="bid" type="radio" value="1" id="1234"/>
<input name="bid" type="radio" value="2" id="5678"/>
<input type="text" id="paymount" name="paymount" value=""/>
</form>

<script type="text/javascript">
var radio = document.radioForm.bid,
input = document.getElementById('paymount');

for(var i = 0; i < radio.length; i++) {
radio[i].onclick = function() {
input.value = this.id;
input.value = this.value;
};
}
</script>
---------------------------------------------------------------------------------------------------------------------
form id 取得 radio 的值

<form action="#" method="post" class="demoForm" id="demoForm">
<fieldset>
<legend>Demo: Get Value Onclick</legend>

<p>Select your size:&nbsp;
<label><input type="radio" name="size" value="5" /> Small</label>
<label><input type="radio" name="size" value="8" checked="checked" /> Medium</label>
<label><input type="radio" name="size" value="12" /> Large</label>
</p>
</fieldset>
</form>

<script>
// get list of radio buttons with name 'size'
var sz = document.forms['demoForm'].elements['size'];

// loop through list
for (var i=0, len=sz.length; i<len; i++) {
sz[i].onclick = function() { // assign onclick handler function to each
// put clicked radio button's value in total field
this.form.elements.size.value = this.value;
};
}
</script>

來源出處 : http://www.dyn-web.com/tutorials/forms/radio/onclick.php

留言