簡體   English   中英

選中素數中的所有復選框

[英]Select all checkbox in primefaces

邏輯要求是:
-我有一個復選框列表(我使用p:selectManyCheckbox)。
-我還有一個復選框,如果選中該復選框,還應該選中其他復選框(為此我使用p:selectBooleanCheckbox)
-讓我舉一個具體的例子,現在我有3個復選框:
+全選
+項目1
+項目2

如果選中“全選”,則選中“項目1”和“項目2”。 如果未選中“全選”,則未選中“項目1”和“項目2”。
如果未選中“項目1”或“項目2”之一,則應選中“全選”。 如果同時選中了“項目1”和“項目2”,則應自動選中“全選”。

為了實現這一點,我將javascript用於每個復選框的onchange事件。
對於全選,onchange = updateOtherCheckboxes(otherCheckbox,selectAllCheckbox)

 function updateCheckboxes(otherCheckboxes, selectAllCheckbox) { otherCheckboxes.inputs.each(function() { if (selectAllCheckbox.isChecked()) { $(this).prop('checked', false); } else { $(this).prop('checked', true); } $(this).trigger('click'); }); } 

對於其他復選框,onchange = updateSelectAllCheckbox(otherCheckbox,selectAllCheckbox)

 function updateSelectAllCheckbox(otherCheckboxes, selectAllCheckbox) { var notAllCheckboxesChecked = false; otherCheckboxes.inputs.each(function() { if ($(this).is(':checked') == false) { notAllCheckboxesChecked = true; return false; } }); if (notAllCheckboxesChecked && selectAllCheckbox.input.is(':checked') == true) { selectAllCheckbox.input.trigger('click'); } else if (!notAllCheckboxesChecked && selectAllCheckbox.input.is(':checked') == false) { selectAllCheckbox.input.trigger('click'); } } 

這里的問題是 ,在updateCheckboxes()函數中,將觸發其他復選框的click事件,並將調用updateSelectAllCheckbox,但我不希望這樣做。 因此,在觸發click事件之后,如何防止在updateCheckboxes()函數中調用updateSelectAllCheckbox()函數。

您不必調用$(this).trigger('check')因為這將觸發click事件,您之前的代碼$(this).prop('checked', true)已確保復選框已選中因此您不必再觸發click事件。

如果$(this).prop('checked', true)不起作用,您也可以嘗試: $(this).attr('checked', true);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM