2

I have radio buttons like this

<input name="show[1]" type="radio" value="1" /> Show<br>
<input name="show[1]" type="radio" value="0" /> Hide<br>

<input name="show[2]" type="radio" value="1" /> Show<br>
<input name="show[2]" type="radio" value="0" /> Hide<br>
...
...
...
<input name="show[n]" type="radio" value="1" /> Show<br>
<input name="show[n]" type="radio" value="0" /> Hide<br>

here length of n can varies.

This is a part of form submit. In jquery i want to make sure one of radio button from each group should be selected. How can i do this

  • 1
    what have you tried until now? by the way, if it's show/hide, why don't you just use one button for each section and use jquery's `toggle` function? – benomatis Mar 24 '14 at 07:40
  • Might help you http://jsfiddle.net/3ews7/ or http://jsfiddle.net/3ews7/1/ – Satpal Mar 24 '14 at 07:44

5 Answers5

1

Try this:

$(':radio').each(function() {
    nam = $(this).attr('name');
    if (submitme && !$(':radio[name="'+nam+'"]:checked').length) {
        alert(nam+' group not checked');
        submitme = false;
    }
});

Working Demo

Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125
0

You can loop through all the numbers for show array and checked whether the radio button is selected or not like as follow.

var n=2;
var allSelected=false;
for(i=1;i<=n;i++){
    if($('[name="show['+n+']"]').is(':checked') == false){
        break;
    }
    allSelected = true;
}
if(allSelected==true){
    alert('All selected');
}
else{
    alert('All not selected');   
}

After looping ,If you get true in allSelected then you can make sure that one of radio button from each group should is selected.

Hope this will help you.

Swapnil Patil
  • 971
  • 2
  • 18
  • 41
0

try this [fiddle][1]

$("#submit").click(function(){
alert($('input[class=show]:radio:not(:checked)').val("1").length)
});

this will return the number of show radio buttons not selected .. so if it alerts n means no show checkboxes have been checked of else if it alerts a number less than n means there means there are checked radio buttons.

Does this help

A J
  • 2,112
  • 15
  • 24
0

This can be easily done using HTML5 requriedReference.
CODE

<form method="post">
    <input name="show[1]" type="radio" value="1" required/>Show
    <br>
    <input name="show[1]" type="radio" value="0" required/>Hide
    <br>
    <input name="show[2]" type="radio" value="1" required/>Show
    <br>
    <input name="show[2]" type="radio" value="0" required/>Hide
    <br>
    <input name="show[n]" type="radio" value="1" required/>Show
    <br>
    <input name="show[n]" type="radio" value="0" required/>Hide
    <br>
    <input type="submit" value="submit" />
</form>

A working example.

Community
  • 1
  • 1
Bhavik
  • 4,836
  • 3
  • 30
  • 45
0

An iterative approach:

HTML

<input name="show[1]" type="radio" value="1" /> Show<br>
<input name="show[1]" type="radio" value="0" /> Hide<br>
<input name="show[2]" type="radio" value="1" /> Show<br>
<input name="show[2]" type="radio" value="0" /> Hide<br>
<input name="show[3]" type="radio" value="1" /> Show<br>
<input name="show[3]" type="radio" value="0" /> Hide<br>
<button>Check</button>

JavaScript:

function check_radio_buttons() {
    var i=1;
    while ($("input[name='show[" + i + "]']").attr("value")) {
        if(!$("input[name='show[" + i + "]']:checked").attr("value")) {
            return false;
        }
        i++;
    }
    return true;
}

$("button").click( function () {
    alert(check_radio_buttons());
});

You must make sure that the inputs as sequential from 1 to n.

Fiddle

Fabricio
  • 839
  • 9
  • 17