简体   繁体   English

没有选中属性的复选框

[英]Checkbox without checked attribute

I have been dynamically adding checkboxes to each row within a table - datatables.net . 我一直在动态地为表格中的每一行添加复选框 - datatables.net。 However, when I check the boxes, the html does not show any checked attribute which then does not allow me to only focus on the rows with checked checkboxes. 但是,当我选中这些框时,html不会显示任何已检查的属性,这样我就不会只关注带有复选框的行。 If I set the checkbox with the checked attribute, then yes, the checked attribute is visible. 如果我使用checked属性设置复选框,则yes,checked元素可见。 The code here shows how I'm trying to check the checked attribute... 这里的代码显示了我如何检查checked属性...

        var filtered_rows = example_data_table._('tr', {"filter":"applied"});

        $.each(filtered_rows, function(i, el){
            // If not checked, do not include
            if(!$(el[0]).is(':checked')){
                console.log(el[0]);
                return true;
            }else{
                window.alert("HIT");
            }
         ...

and the following is how I'm appending the element to the datatables.net table... 以下是我如何将元素附加到datatables.net表...

        var checkbox = $("<input type='checkbox' 
                           id='checkbox_" + o.example_id + "' />");

        ...

        tmp = [checkbox[0].outerHTML,
        ...];
        table_data.push(tmp);

        ...

        example_data_table.fnClearTable(false);
        example_data_table.fnAddData(table_data);

In the console, all I get is this message n times: 在控制台中,我得到的是这个消息n次:

<input type="checkbox" id="checkbox_3895">                  examples.php:189


I guess, I'm wondering why the checked attribute is never included even when I have checked the checkbox? 我想,我想知道为什么即使我选中了复选框,也不会包含checked属性?

The HTML markup attribute <input checked="" is not actually designed to show the state of the element at runtime moment, but to define the default state of it when the page renders for the first time . HTML标记属性<input checked=""实际上并不是为了在运行时刻显示元素的状态,而是为了在第一次呈现页面时定义它的默认状态

MDN < input > docs states: MDN <input> docs说明:

checked: 检查:

When the value of the type attribute is radio or checkbox, the presence of this Boolean attribute indicates that the control is selected by default; 当type属性的值为radio或checkbox时,此Boolean属性的存在表示默认选择控件; otherwise it is ignored. 否则会被忽略。

The only varying of checked state you can target is the Js DOM property checked : 您可以定位的唯一不同的已检查状态是已checked的Js DOM属性:

document.getElementById('myInput').checked; //(true / false)

or the Css pseudo :checked : 或者Css伪:checked

#myInput:checked {
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM