簡體   English   中英

用javaScript更改輸入復選框的選中屬性

[英]changing input checkbox checked attribute witth javaScript

我正在嘗試使用jQuery更改輸入的值。 值正在正確更改,但輸入框上的標記/取消標記未顯示...我嘗試過是否存在位置或填充問題,但這是不同的問題,我有點堆砌。

這是更改輸入值的代碼:

    $('.AlarmsTreeView-Title input').click(function () {
        var isChecked = $(this).val();
        if (isChecked == "false") {
            $(this).attr('value', 'true').attr('checked','true');
        } else {
            $(this).attr('value', 'false').removeAttr('checked');
        }
    });  

如果您使用.attr()則應該

attr('checked','checked');

建議您使用.prop()而不是.attr()進行檢查,因為您要訪問屬性 “ checked”。 因此,您可以使用

$(this).prop('checked',true);

$(this).prop('checked',false);

請查閱prop()的文檔以獲取更多說明。

怎么樣:

    var isChecked = $(this).is(':checked');
    if (isChecked) {
        //do something
    } else {
        //do something
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM