最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Checkbox without checked attribute - Stack Overflow

programmeradmin7浏览0评论

I have been dynamically adding checkboxes to each row within a table - datatables . 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. If I set the checkbox with the checked attribute, then yes, the checked attribute is visible. The code here shows how I'm trying to check the checked attribute...

        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 table...

        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:

<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?

I have been dynamically adding checkboxes to each row within a table - datatables . 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. If I set the checkbox with the checked attribute, then yes, the checked attribute is visible. The code here shows how I'm trying to check the checked attribute...

        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 table...

        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:

<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?

Share Improve this question edited Sep 29, 2014 at 20:02 Blazemonger 93.1k27 gold badges145 silver badges181 bronze badges asked Sep 29, 2014 at 19:58 QuintoVientoQuintoViento 1984 silver badges19 bronze badges 3
  • 6 The checked attribute shouldn't be shown, you're just not looking in the right place, it's the checked property that is changed, and that's not visible in the markup. – adeneo Commented Sep 29, 2014 at 19:59
  • 1 @adeneo is right, if you add it like this <input type="checkbox" id="checkbox_3895" checked="checked"> you will see that the attribute checked will not change when you do check/uncheck. Try it here jsfiddle/pm9rat56 – Bojan Petkovski Commented Sep 29, 2014 at 20:13
  • Thanks, that definitely makes sense – QuintoViento Commented Sep 29, 2014 at 20:21
Add a ment  | 

1 Answer 1

Reset to default 6

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.

MDN < input > docs states:

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; otherwise it is ignored.

The only varying of checked state you can target is the Js DOM property checked:

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

or the Css pseudo :checked:

#myInput:checked {
}
发布评论

评论列表(0)

  1. 暂无评论