迷失在节点的海洋中

时间:2014-03-26 23:16:20

标签: javascript jquery

我有一个应该复制表单并清除它的函数。除了复选框之外,它会清除所有内容。为什么是这样? http://jsfiddle.net/GM2GN/1/

    function addForm(btn, prefix) {
            var formCount = parseInt($('#id_' + prefix + '-TOTAL_FORMS').val());
            // You can only submit a maximum of 10 todo items 
            if (formCount < 100) {
                // Clone a form (without event handlers) from the first form
                var row = $(".item:first").clone(false).get(0);
                // Insert it after the last form
                $(row).removeAttr('id').hide().insertAfter(".item:last").slideDown(300);

                // Remove the bits we don't want in the new row/form
                // e.g. error messages
                $(".errorlist", row).remove();
                $(row).children().removeClass("error");

                // Relabel or rename all the relevant bits
                $(row).children().children().children().children().each(function () {
                    updateElementIndex(this, prefix, formCount);
                    $(this).val("");
                });

                // Add an event handler for the delete item/form link 
                $(row).find(".delete").click(function () {
                    return deleteForm(this, prefix);
                });
                // Update the total form count
                $("#id_" + prefix + "-TOTAL_FORMS").val(formCount + 1);
            } // End if
            else {
                alert("Sorry, you can only enter a maximum of 100 items.");
            }
            return false;
        }
        // Register the click event handlers
        $("#add").click(function () {
            return addForm(this, "form");
        });

2 个答案:

答案 0 :(得分:1)

与文本输入等其他输入元素不同,更改复选框值不会更改“已检查”状态。 val()仅更改提交给服务器的复选框的值。更改已检查状态:

checkBoxes.prop("checked", false);

答案 1 :(得分:0)

以上是完整的修改代码..

只是检查你正在重置的实际输入是否是一个复选框,在这种情况下,$(this).val('')会将其值设置为空字符串,但这不会删除它的“checked”属性。

我刚刚在$(this).val('') :

之后添加了此项检查
if ($(this).is('input[type="checkbox"]')) {
    $(this).removeAttr("checked");
} 

修改后的代码:

$(document).ready(function () {
            // Code adapted from http://djangosnippets.org/snippets/1389/  
            function updateElementIndex(el, prefix, ndx) {
                var id_regex = new RegExp('(' + prefix + '-\\d+-)');
                var replacement = prefix + '-' + ndx + '-';
                if ($(el).attr("for")) $(el).attr("for", $(el).attr("for").replace(id_regex,
                replacement));
                if (el.id) el.id = el.id.replace(id_regex, replacement);
                if (el.name) el.name = el.name.replace(id_regex, replacement);
            }

        function deleteForm(btn, prefix) {
            var formCount = parseInt($('#id_' + prefix + '-TOTAL_FORMS').val());
            if (formCount > 1) {

                $(btn).parents('.item').remove();            

                var forms = $('.item'); // Get all the forms  
                // Update the total number of forms (1 less than before)
                $('#id_' + prefix + '-TOTAL_FORMS').val(forms.length);
                var i = 0;
                // Go through the forms and set their indices, names and IDs
                for (formCount = forms.length; i < formCount; i++) {
                    $(forms.get(i)).children().children().children().children().each(function () {
                        if ($(this).attr('type') == 'text') updateElementIndex(this, prefix, i);
                    });
                }
            } // End if
            else {
                alert("You have to enter at least one student!");
            }
            return false;
        }

        function addForm(btn, prefix) {
            var formCount = parseInt($('#id_' + prefix + '-TOTAL_FORMS').val());
            // You can only submit a maximum of 10 todo items 
            if (formCount < 100) {
                // Clone a form (without event handlers) from the first form
                var row = $(".item:first").clone(false).get(0);
                // Insert it after the last form
                $(row).removeAttr('id').hide().insertAfter(".item:last").slideDown(300);

                // Remove the bits we don't want in the new row/form
                // e.g. error messages
                $(".errorlist", row).remove();
                $(row).children().removeClass("error");

                // Relabel or rename all the relevant bits
                $(row).children().children().children().children().each(function () {
                    updateElementIndex(this, prefix, formCount);
                    $(this).val("");
                    if ($(this).is('input[type="checkbox"]')) {
                     $(this).removeAttr("checked");
                    }
                });

                // Add an event handler for the delete item/form link 
                $(row).find(".delete").click(function () {
                    return deleteForm(this, prefix);
                });
                // Update the total form count
                $("#id_" + prefix + "-TOTAL_FORMS").val(formCount + 1);
            } // End if
            else {
                alert("Sorry, you can only enter a maximum of 100 items.");
            }
            return false;
        }
        // Register the click event handlers
        $("#add").click(function () {
            return addForm(this, "form");
        });

        $(".delete").click(function () {
            return deleteForm(this, "form");
        });
    });