3

当我单击具有此复选框的元素跨度时,我需要设置项目复选框。

代码:

<script>
$('.CheckMemberClassList').bind('click',function(){
    $('.CheckMemberClassList').removeClass('MemberClassActive').addClass('MemberClassInactive');
    $(this).removeClass('MemberClassInactive').addClass('MemberClassActive');
    });
</script>


<span class="MemberClass1 MemberClassActive CheckMemberClassList">
<input type="radio" name="MemberClassList" id="MemberClassList1" value="1" checked="checked">
Text One
</span>

<span class="MemberClass2 MemberClassInactive CheckMemberClassList">
<input type="radio" name="MemberClassList" id="MemberClassList2" value="2">
Text Two
</span>

<span class="MemberClass3 MemberClassInactive CheckMemberClassList">
<input type="radio" name="MemberClassList" id="MemberClassList3" value="3">
Text Tree
</span>

请告诉我我需要如何在这段代码中制作它?

4

3 回答 3

8

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>
于 2012-10-12T00:20:49.543 回答
2
$('.CheckMemberClassList').bind('click',function(){
    $('.CheckMemberClassList').removeClass('MemberClassActive').addClass('MemberClassInactive');
    $(this).removeClass('MemberClassInactive').addClass('MemberClassActive');
    $(this).find('input:radio').prop('checked', true);
});

工作示例:http: //jsfiddle.net/hMYtg/

Jquery .prop() 文档:http ://api.jquery.com/prop/

于 2012-10-12T00:19:41.493 回答
0

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();
});​
于 2012-10-12T00:21:03.780 回答