1

我想用复选框做这样的事情。如果用户单击复选框,它应该更改其状态(选中 -> 未选中和 vv. )。

我的代码:

$('#checkBoxStandard').change(function() {
    clickedFormBoxen('standard');
});


function clickedFormBoxen(active){
if(active == 'standard'){

    if( $('#checkBoxStandard').is(":checked")){

        $('#checkBoxStandard').prop("checked", false);

    }else{

        $('#checkBoxStandard').prop("checked", true);
    }

    console.log('ac: '+$('#checkBoxStandard').is(':checked'));
}

不幸的是,该复选框不会再次被取消选中。第一次,复选框被选中,但如果我再次点击它,没有任何反应,它仍然被选中。

我希望使用此代码,因此我可以通过函数调用而不仅仅是通过用户交互来更改复选框的状态。

请帮助我,对不起我的英语^^

4

3 回答 3

1

尝试

$('#checkBoxStandard').removeAttr("checked");
于 2013-07-11T16:55:56.967 回答
0

你的意思是这样的?( jsFiddle )

HTML

<input type="checkbox" id="checkbox">
<label for="checkbox">Hey,check me!</label>

JavaScript

var respond = true;

function manualCheck(state)
{
    respond = false;
    $('#checkbox').prop("checked", state);
}

$('#checkbox').change(function ()
{
    if (!respond)
    {
        respond = true;
        return;
    }

    // Your code
}
于 2013-07-11T17:38:58.497 回答
0

正如我在对您的问题的评论中提到的那样,通过您的功能,clickedFormBoxen您可以有效地恢复用户交互对复选框元素的影响。因此,您似乎必须从复选框元素上的单击处理程序调用更改处理程序(我已经简化了代码):

function clickedFormBoxen(active) {
    if (active == 'standard') {
        $('#checkBoxStandard').prop("checked", !($('#checkBoxStandard').prop("checked")));
    }
}

$(document).ready( function(){
    $('#checkBoxStandard').change( function(e) {
        clickedFormBoxen('standard');
        1;
    });

    $('#checkBoxStandard').click(function(e) {
        $('#checkBoxStandard').change();
        1;
    });
});
于 2013-07-11T18:31:24.113 回答