How to get all unchecked radio buttons

前端 未结 4 1036
别跟我提以往
别跟我提以往 2021-01-11 10:44

Ok, so I\'ve got a jQuery code which constructs my radio inputs from XML data, like this:

var items = xml.children(\'item\');
if (items.length > 0)
{
             


        
相关标签:
4条回答
  • 2021-01-11 11:10

    Found it myself. None of the above answers worked for me, which is strange, because most of them should be totally legit.

    What I found to be working is actually

    $('input[type="radio"]:not(:checked)')
    

    And in my case I needed

    $('li input[type="radio"]:not(:checked) + label')
    

    And the whole code is:

    //we'll need m for detecting a click outside of element with our radio buttons...
    var m = false;
    $(document).ready(function(){
        $.ajax({
            type: "GET", url: 'xml.xml', dataType: 'xml',
            success: function(data){
                var xml = $(data);
                $('#windowList').append( ItemToUl(xml.children()) );
            }
        });
        $('.calc').hover(function(){ 
            m=true; 
        }, function(){ 
            m=false; 
        });
        $("body").mousedown(function(){ 
            if(! m) { 
                //...and unchecking a checked radio. I heard that .attr() was deprecated but couldn't get .prop() to work
                $('li input[type="radio"]:checked').attr('checked', false);
                $('li input[type="radio"]:not(:checked) + label').fadeTo('slow', 1);
            }
        });
        $("li input").live("change", function(){
            $('li input[type="radio"]:checked + label').fadeTo('slow', 1);
            $('li input[type="radio"]:not(:checked) + label').fadeTo('slow', 0.45);
        }); 
    });
    
    //constructing function, etc...
    
    0 讨论(0)
  • 2021-01-11 11:15

    i think this sould work

    $("input:!checked");
    

    You may want to put a class selector in there as well.

    Then do an .each to do something with the elements

    0 讨论(0)
  • 2021-01-11 11:22

    Try this (also see my jsfiddle):

    $('input').not(':checked')
    
    0 讨论(0)
  • 2021-01-11 11:24
    $('form input[type="radio"]').not(':checked').each(function() {
        $(this).addClass('unchecked');
        // or
        $(this).slideUp('slow');
    });
    

    Obviously the element you use is up to you but the :unchecked selector is what you're after.

    0 讨论(0)
提交回复
热议问题