2014-03-26 48 views
1

的海上失蹤我有一個應該複製形式,並且清除它的功能。它將清除除複選框之外的所有內容。爲什麼是這樣? 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"); 
     }); 

回答

1

從其它輸入元素,如文本輸入,更改複選框值不會改變不同的「檢查」狀態。 val()僅更改提交給服務器的複選框的值。改變檢查狀態:

checkBoxes.prop("checked", false); 
+0

謝謝!我還在刪除函數中看到了「type = text」條件,並更改了它。 – broinjc

0

以上是修改後的完整代碼..

只是檢查,如果你重置實際輸入是一個複選框,在這種情況下,$(本).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"); 
     }); 
    }); 
相關問題