开发者

Trying to get value of radio box when you click on them

开发者 https://www.devze.com 2023-01-20 12:13 出处:网络
i am trying to get the value of the raido boxes when you click on them. I am using jquery i have been looking at change. click. i cant get any of them to work.

i am trying to get the value of the raido boxes when you click on them. I am using jquery

i have been looking at change. click. i cant get any of them to work.

 <table> 
  <tr> 
<td>Some text 1 </td> 
<td><input type="radio" value="txt1" name="myRadio" id="myRadio" /></td> 
<td>Some text 2 </td&开发者_Python百科gt; 
<td><input type="radio" value="txt2" name="myRadio" id="myRadio" /></td> 
<td>Some text 3 </td> 
<td><input type="radio" value="txt3" name="myRadio" id="myRadio" /></td> 
  </tr> 
 </table>


Depends on how you try to get the value from them ...

using the .val() inside the click event works just fine..

example at http://www.jsfiddle.net/aKTcu/

$('input[type=radio]').click(function(){
   alert( $(this).val() );
   // or this.value;
});

but your ID's should be unique regardless, as it is invalid HTML otherwise..


A different id value should be used for each element something you are not doing and using myRadio as id for multiple elements - a reason why your script is not working.

Alternatively, you can use the class or different id values. Once you do that, you can modify your jQuery code accordingly.


The change event should work fine. You just need to look at this.value.

$("input").change(function(){
    alert(this.value);
});​

I've made a jsFiddle to show this working in action.


If you have more that on input in your page you can use a different selector:

$('input[name="myRadio"]').click(function(){
    alert(this.value);
});


You can try to ask for the checked attribute, see the example:

<label for="public0"><input type="radio" checked="checked" name="publicar" id="public0" value="TRUE" /> YES</label>

  <label for="public1"><input type="radio" name="publicar" id="public1" value="FALSE" /> NO</label>

And then ask for it:

if ( $("public0").checked == true) 
{ ...} or if ( $("public1").checked == true){...}

You can see the values:

 //alert($("public0").checked);
  //alert($("public1").checked);

Of course you can add more input types=radio, just remember that for xhtml the id for objects should be different.

0

精彩评论

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