1

单击添加按钮时,我正在使用最后一行的克隆添加新行,但如果它已经在最后一行中选中,则希望取消选中复选框。

请检查整个代码:http: //jsfiddle.net/MMDZX/

4

6 回答 6

2

将虚线更改为:

jQuery("#dataTable tr:last").find("input[type='checkbox']").attr("checked", false);

两个问题:

  • 您正在搜索错误类型的输入元素(type=text而不是type=checkbox)。
  • 您使用的是字符串而不是布尔值来设置属性,如果您将其设置为选中状态会很好,因为字符串是真实的。

此外,.children().children()是不必要的。.find() (或祖先-后代)在这种情况下更漂亮。

演示。

于 2012-05-10T14:58:49.550 回答
1

在此处查看工作代码:http: //jsfiddle.net/3rLqV/

将删除检查状态行更改为:

jQuery("#dataTable tr:last input[type='checkbox']").removeAttr("checked");

您的两个主要问题是您正在寻找type='text',而不是复选框,并且您不能设置checked为 false。它存在或不存在。

于 2012-05-10T14:59:28.333 回答
0

To remove checked status of checkbox when you clone last row.

You can add following line,

jqasdasuery.find("input[type='checkbox']").attr('checked',false);

After

var jqasdasuery = jQuery("#dataTable tr:last").clone();

in "#addRow" click function.

于 2012-05-10T15:26:19.597 回答
0
  $(document).ready(function () {
            $("#addRow").on('click', function () {
                var jqasdasuery = $("#dataTable tr:last").clone();

                $("#dataTable tbody").append(jqasdasuery);
                $("#dataTable tr:last").find(':checkbox').attr('checked', false);
                $("#dataTable tr:last").find('select option:first').attr('selected', true); 
                setAttributes();
            });

            $("#deleteRow").on('click', function () {
                if ($("#dataTable tr").length === $("#dataTable input:checked").length) {
                    alert("You cant delete all the rows")
                } else {
                    $("#dataTable").find(':checked').each(function () {
                        $(this).parents('tr').remove();
                    });
                }

                setAttributes();
            });
        });

        function setAttributes() {
            $("#dataTable tr").each(function (i) {
                $(this).find(":checkbox").attr("name", "chk" + i);
                $(this).find(":text").attr("name", "txt" + i);
                $(this).find("select").attr("name", "country" + i);
            });

            //$("#dataTable tr:last").find("input[type='text']").attr("checked", "false");
        };
于 2012-05-10T15:14:45.657 回答
0

尝试这个

 // to add new row
        jQuery("#addRow").click(function () {
            var jqasdasuery = jQuery("#dataTable tr:last").clone();
            jqasdasuery.find('input[type=checkbox]').attr("checked", false);
            jQuery("#dataTable").append(jqasdasuery);
            setAttributes();
        });
于 2012-05-10T14:59:42.870 回答
0

添加

$(':checkbox',jqasdasuery).attr('checked', false);

克隆之后。

于 2012-05-10T14:59:58.227 回答