2013-09-23 55 views
0

我在每一行都有一個複選框。我有一個按鈕,刪除複選框被選中的所有行。爲此,我必須得到複選框被選中的行的標識。我寫下了下面的代碼。但不工作。請幫助我走出傢伙..點擊按鈕獲取列表中選中複選框的標識

按鈕代碼:<a class="row-delete" align="left" onclick="deleteItem()"/>

function deleteItem(){ 
    if (confirm("Are you sure you want to delete?")){    
     $(":checkbox").change(function() { 
      var notChecked = [], checked = []; 
      $(":checkbox").map(function() { 
       this.checked ? checked.push(this.id) : notChecked.push(this.id); 
      }); 
      alert("checked: " + checked); 
     }); 
     deleteAll(checked,0); 
    } 
} 

什麼不對上面的代碼?

+2

檢查= [];並且this.checked可能被限制 –

+0

不知道該代碼有什麼問題(除了使用'.map'不正確),但是你可以直接獲得'$(「:checkbox:checked」)。get()'to獲取所有選中的框 – CodingIntrigue

+0

$(「:checkbox:checked」)。get()..我應該在$(「:checkbox」)的地方使用map(function()) –

回答

1

您註冊在不需要deleteItem方法change事件處理程序,您需要使用。每次通過複選框來只是想迭代()和更新所需的陣列

而且最好是使用.each()代替的.map()因爲你不返回任何值

function deleteItem(){ 
    if (confirm("Are you sure you want to delete?")){    
     var notChecked = [], checked = []; 
     $(":checkbox").each(function() { 
      if(this.checked){ 
       checked.push(this.id); 
      } else { 
       notChecked.push(this.id); 
      } 
     }); 
     alert("checked: " + checked); 
     deleteAll(checked,0); 
    } 
} 
1

deleteItem刪除onchange function因爲link當你click它將re-init上茶的checkbox elements

function deleteItem(){ 
    if (confirm("Are you sure you want to delete?")){    
     var notChecked = [], checked = []; 
     $(":checkbox").each(function() { 
      id=this.id; 
      this.checked ? checked.push(id) : notChecked.push(id); 
     }); 
     alert("checked: " + checked); 
     deleteAll(checked,0); 
    } 
} 

NGE您可以創建一個array of checked checkboxes一樣,

$(":checkbox:checked").each(function() { 
     checked.push(this.id); 
    }); 

如果你想刪除項目的onclick複選框然後嘗試像的,

$(":checkbox").on('click',function() { 
    if($(this).is(':checked')){ 
     alert($(this).attr('id'));// this item is ready to delete 
    } 
}); 
+0

我需要從Delete所有按鈕,我必須通過id的行deleteAll方法.. –

+1

好吧,然後不要使用我的'答案''最後一個'方法 –

相關問題