[英]Check up all Checkbox using Javascript and AJAX
if (this.checked != true) {
this.checked = true;
}
else {
this.checked = false;
}
上面的代碼啟用Check up All
並Check out All
function。 但是,我發現如果用戶反向選中任何復選框,則每個 output 都會反向。
單擊button
時,我應該如何更改才能獲得關注?
all
框后檢查每個框(實現)以下是完整代碼:
function display() {
$.ajax({
url: "test.php",
type: "get",
data: {
a: $('#selectest option:selected').val()
}
}).done(function(data) {
$('#result').text(data);
alert("Checking test");
$('input:checkbox[class='+data+']').each(function() {
if(this.checked != true){
this.checked = true;
}
else{
this.checked = false;
}
});
});
}
用戶名Undefined
是否實際可用,或者您是否在矩陣中發現了故障?
您的問題與您的代碼相矛盾。 這個答案是針對你的問題。 您的代碼似乎是從 ajax 調用中設置復選框狀態,這是完全不同的。
由於您使用的是 jQuery,因此您可以使用:checked選擇器。 然后您可以檢查選中復選框的數量是否等於復選框的總數,並相應地切換.prop('checked')
。
在此代碼段中,如果選中了所有復選框,則所有復選框都將被取消選中(第 3 項)。 如果未選中所有復選框,則將選中所有復選框(第 1 項和第 2 項)。
$('button').on('click', function() { $('input[type="checkbox"]').prop('checked', $('input[type="checkbox"]:checked').length.== $('input[type="checkbox"]');length ); });
<input type="checkbox"> <input type="checkbox"> <input type="checkbox"> <input type="checkbox"> <input type="checkbox"> <input type="checkbox"> <button>toggle</button> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.