views:

1166

answers:

2

I have MVC html control for radiobutton like:

 <%= Html.RadioButton("Choice", false, new { onclick = "Accept()" })%><label for="Choice">Yes</label>
 <%= Html.RadioButton("Choice", false, new { onclick = "Deny()" })%><label for="Choice">No</label>

How to get radio button selection with JQuery?

+3  A: 

I have no idea what the html will look like that your code makes. But, you can use this code to get each radio button's value. you can write a more specific selector yourself.

$("input:radio").each(function(){
 if($(this).is(":checked")){
   var val = this.value;
 } else {
   //not checked. do something else.
 }
});

or get all the radios that are checked:

$("input:radio:checked").each(function() {
   var value = this.value;
   //do something with value of checked radiobox
});
mkoryak
A: 

If my understand is right, second parameter must be object's value, and third parameter is checked state.

so, it must be.

 <%= Html.RadioButton("Choice", "Yes", false, new { onclick = "Accept()" })%><label for="Choice">Yes</label>
 <%= Html.RadioButton("Choice", "No", false, new { onclick = "Deny()" })%><label for="Choice">No</label>

then you can select by jquery.

$("Choice[value='Yes']").attr("checked", true);

Update: or if you want to get a value.

var yes = $("Choice[value='Yes']).val();
Jirapong
this will set a radio button to be checked. i dont think that is what he wanted
mkoryak