1

I have a multiple rows of radio boxes like this

<input id="m_0" type="radio" value="m" name="c_0">
<input id="f_0" type="radio" value="f" name="c_0">

<input id="m_1" type="radio" value="m" name="c_1">
<input id="f_1" type="radio" value="f" name="c_1">

<input id="m_2" type="radio" value="m" name="c_2">
<input id="f_2" type="radio" value="f" name="c_2">

So, I want to validate if the radio has been selected in each row

I tried this way - if ($('input:radio[name="c_"+i]:checked').val()) {

where i = 0,1,2... but it didn't work

any idea how can I change it to work? thanks.

rkt
  • 1,171
  • 2
  • 9
  • 18
  • Have a look at http://stackoverflow.com/questions/277589/validation-of-radio-button-group-using-jquery-validation-plugin – Adam Nov 16 '12 at 10:40

2 Answers2

1

val returns a string which is always true, you should use length property instead:

if ($('input:radio[name=c_'+i+']:checked').length) {

Note that you are concatenating the variable's value incorrectly.

Ram
  • 143,282
  • 16
  • 168
  • 197
  • this didn't work, for the first row, i selected m_0, but still it is going to else – rkt Nov 16 '12 at 10:59
  • What is `i` in your code? Can you provide a demo on jsfidde.net? – Ram Nov 16 '12 at 11:00
  • your first answer actually worked with slight modification : if ($('input:radio[name=c_'+i+']:checked').length) { thanks – rkt Nov 16 '12 at 11:50
0

Use the code:

$("input[type=radio]").prop("checked") )
$("input[type=radio]").is(":checked") )
Irvin Dominin
  • 30,819
  • 9
  • 77
  • 111