How set item checkbox when i click on element span which have this checkbox?

前端 未结 3 1301
抹茶落季
抹茶落季 2021-01-21 05:11

Me need set item checkbox when i click on element span which have this checkbox.

Code:



        
相关标签:
3条回答
  • 2021-01-21 05:26

    First of all, do you want a checkbox or a radio input? Your code is a radio input.

    Here is a jsfiddle showing what you require

    You need the following javascript:

    $('span').click(
        function() {
            var cb = $(this).find(":radio")[0];
    
            if (!$(cb).attr("checked")) {
                $(cb).attr("checked", "checked");
            } else {
                $(cb).removeAttr("checked");
            }
        }
    );
    
    $('span input').click(
        function(event){
            event.stopPropagation();
    });​
    
    0 讨论(0)
  • 2021-01-21 05:36

    change your <span>s to <label> with a for= attribute matching the ID of your input, like so:

    <label for="checkbox1">Click Me!</label>
    <input type="checkbox" id="checkbox1">
    

    http://jsfiddle.net/2GbPS/

    Actually, since you have your checkbox inside the span, this will work:

    <label>
    <input type="checkbox">
    Click Me!
    </label>
    

    (fiddle updated)

    Works with radios too!

    <label>
    <input type="radio">
    Click Me!
    </label>
    
    0 讨论(0)
  • 2021-01-21 05:39
    $('.CheckMemberClassList').bind('click',function(){
        $('.CheckMemberClassList').removeClass('MemberClassActive').addClass('MemberClassInactive');
        $(this).removeClass('MemberClassInactive').addClass('MemberClassActive');
        $(this).find('input:radio').prop('checked', true);
    });
    

    Working Example: http://jsfiddle.net/hMYtg/

    Jquery .prop() Documentation: http://api.jquery.com/prop/

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