admin管理员组

文章数量:1384604

Very simple , this st**id tinny things will kill me.

I trying loop each radio button.

$('#recover input:radio:checked').each(function() {
  alert("checked");
});

OR

 function Checkform() {
    var result = true;
    $('#recover input[type=radio]').each(function() {
        var checked = $(this).find('input:radio:checked');
        if (checked.length == 0) {
            result = false;
            alert ("check");
        }
    });
    return result;
}

OR

 $('#recover input[type=radio]').each(function(){
 if($(this).attr('checked')){
    alert ("check");
  }
});

HTML :

<div id="recover">
  <input type="radio" name="s">
  <input type="radio" name="s">
  <input type="radio" name="s">
</div>

tryed also :

<div id="recover">
<form>
  <input type="radio" name="s">
  <input type="radio" name="s">
  <input type="radio" name="s">
</form>
</div>

And:

<div id="recover">
<form>
  <input type="radio" name="s" value="1">
  <input type="radio" name="s" value="2">
  <input type="radio" name="s" value="2">
</form>
</div>

And more bination of HTML .

And tryed more like 5 other examples of jQuery / Javascript, none working and i dont know why .

Any help please , thanks allot.

Very simple , this st**id tinny things will kill me.

I trying loop each radio button.

$('#recover input:radio:checked').each(function() {
  alert("checked");
});

OR

 function Checkform() {
    var result = true;
    $('#recover input[type=radio]').each(function() {
        var checked = $(this).find('input:radio:checked');
        if (checked.length == 0) {
            result = false;
            alert ("check");
        }
    });
    return result;
}

OR

 $('#recover input[type=radio]').each(function(){
 if($(this).attr('checked')){
    alert ("check");
  }
});

HTML :

<div id="recover">
  <input type="radio" name="s">
  <input type="radio" name="s">
  <input type="radio" name="s">
</div>

tryed also :

<div id="recover">
<form>
  <input type="radio" name="s">
  <input type="radio" name="s">
  <input type="radio" name="s">
</form>
</div>

And:

<div id="recover">
<form>
  <input type="radio" name="s" value="1">
  <input type="radio" name="s" value="2">
  <input type="radio" name="s" value="2">
</form>
</div>

And more bination of HTML .

And tryed more like 5 other examples of jQuery / Javascript, none working and i dont know why .

Any help please , thanks allot.

Share Improve this question edited Oct 1, 2013 at 6:17 asdasd asda asked Oct 1, 2013 at 6:12 asdasd asdaasdasd asda 651 silver badge9 bronze badges 4
  • 1 What's the goal ? BTW, "#recovery" won't work if the id is "recover". – Denys Séguret Commented Oct 1, 2013 at 6:14
  • recover and recovery :P..hope you got it – coolguy Commented Oct 1, 2013 at 6:16
  • I need to loop each radio button O.o, the ID is not recovery its recover maybe spelling mistake in StackOverFlaw question i will fix it. – asdasd asda Commented Oct 1, 2013 at 6:17
  • $('#recovery input[type=radio]').each(function(){ if($(this).attr('checked')){alert ("check");}}); - This code is right. See the fiddle - jsfiddle/saranyaciet/AmEZ4 – Saranya Sadhasivam Commented Oct 1, 2013 at 6:20
Add a ment  | 

6 Answers 6

Reset to default 4

Use $(this).prop('checked') instead of $(this).attr('checked')

jsFiddle Demo


Attributes vs. Properties

...

Nevertheless, the most important concept to remember about the checked attribute is that it does not correspond to the checked property. The attribute actually corresponds to the defaultChecked property and should be used only to set the initial value of the checkbox. The checked attribute value does not change with the state of the checkbox, while the checked property does. Therefore, the cross-browser-patible way to determine if a checkbox is checked is to use the property:

if ( elem.checked )
if ( $( elem ).prop( "checked" ) )
if ( $( elem ).is( ":checked" ) )

The same is true for other dynamic attributes, such as selected and value.

TRy this Demo http://jsfiddle/devmgs/X6QhN/

function doCheck(){
   $('#recover input[type="radio"]:checked').each(function() {
  alert("checked");
});
}

use prop because it gives true or false

and attr

gives property name like checked or not so cant use in condition

   $('#recovery input[type=radio]').each(function(){
     if($(this).prop('checked')){
        alert ("check");
      }
    });

reference prop

$(document).ready(function(){
 $('#recover > radio').each(function(){
     if($(this).prop('checked')){
        alert ("checked");
      }
    });
    });

Try This `

               jQuery(function ()
               {
        if (jQuery('#recover input[name="s"]').is(':checked'))
        {
               alert("Checked");            }
        else
        {

             alert("Please select an Record");
          }
    });`

You can access the checked property directly from the dom reference

$('#recover input[type=radio]').each(function () {
    if (this.checked) { // or $(this).is(':checked')
        alert("check");
    }
});

If you want to process only checked items then use the :checked selector

$('#recover input[type=radio]:checked').each(function () {
    alert("check");
});

Demo: Fiddle

本文标签: javascriptjQuery loop each radio button not workingStack Overflow