开发者

how find radio button value

开发者 https://www.devze.com 2023-01-28 21:29 出处:网络
i have a four radio buttons in a row. so i have 10 rows in a table. all radio buttons in a row having same id.

i have a four radio buttons in a row. so i have 10 rows in a table. all radio buttons in a row having same id. in one time, only one radio button in a row can be selected. some radio buttons are checked and some non checked. i want to put a validation when submit the form. when i submit the form if all radio button is non selected in a row then a msg box can be appear. i have found the radio button id but i am not able to get their value, either true or false. it show me in a alert box: radiobutton Value is: undefined

here is the method:

function showValue() {
var  radioID="radiobutton";
var radiobuttonName;
var radiobuttonValue;
   for (var i=0;i<10;i++) {
  radiobuttonName=radioID+i;
  alert("radiobutton Name is:"+radiobuttonName);
  if (radiobuttonName.checked)
  {
      radiobuttonValue=radiobuttonName.value
  }
 alert("radiobutton Value is:"+radiobuttonValue);
    }
}

Edit:

i have four radio button in a row. name and id is same of all four button in a row. suppose, name and id for all buttons in first row is radiobutton0. for second row it is radiobutton1, for third row it is radiobutton2. thus i have 10 rows in a table. my aim is when i submit the page, one button must be checked in a row else it show a msg box that please fill the corresponding radio button. . i want to get the value of radio button either true or false.

i run the following code:function get_radio_value() { var radioID="radiobutton"; var radiobuttonName; var radiobuttonValue; for (var i=0;i<10;i++) { radiobuttonName=radioID+i; alert("radiobutton Name is:"+:"+radiobuttonName); } } this method execute the loop and display all radiobutton Names. How i find radiobuttonValue which is either true or fal开发者_高级运维se.


You're trying to alert the .value property of a string (radiobuttonName is just a string, not an element), which is undefined, instead you need something like this:

function showValue() {
  var radioID="radiobutton", radiobuttonName, radiobuttonValue, rb;
   for (var i=0;i<10;i++) {
     radiobuttonName=radioID+i;
     rb = document.getElementsByName(radiobuttonName)[0];
     alert("radiobutton Name is:"+radiobuttonName);
     if (rb.checked)
     {
        radiobuttonValue=rb.value
     }
     alert("radiobutton Value is:"+radiobuttonValue);
   }
}

In the above we're getting the first element with that name (I'm guessing by your naming conventions), if it had an ID for example, you'd replace document.getElementsByName(radiobuttonName)[0] with document.getElementById(radiobuttonName).

0

精彩评论

暂无评论...
验证码 换一张
取 消