[英]How can I Write this in JQuery
if ( !document.getElementById('chkBoxView').checked
&&!document.getElementById('chkBoxEdit').checked
&&!document.getElementById('chkBoxAdd').checked
&&!!document.getElementById('chkBoxDelete').checked )
{
alert('Please select atleast one checkBox');
return false;
}
else {
// Atleast One Check Box Selected By User.
return true
}
上面是JavaScript,JQuery中對應和優化的代碼是什么?
if (!$(":checkbox[id^=chkBox]:checked").length) {
alert('Please select atleast one checkBox');
return false;
}
alert('you filled at least one!');
return true;
由於您所有的復選框ID均以'chkBox'開頭,因此您可以使用屬性Starts With selector對其進行尋址。 選中復選框的真實長度表示至少已選中一個。
if (
!jQuery('#chkBoxView').is(':checked')
&&!jQuery('#chkBoxEdit').is(':checked')
&&!jQuery('#chkBoxAdd').is(':checked')
&&!jQuery('#chkBoxDelete').is(':checked')
) {
alert('Please select atleast one checkBox');
return false;
}
else {
// Atleast One Check Box Selected By User.
return true
}
如果您更喜歡$
不是jQuery
也可以使用它。
您可以嘗試以下方法:
$("input:checked[id=yourid]").val()
這是好方法
jQuery('#chkBoxView').is(':checked')
您可以使用多重選擇器來獲取每個復選框(如果已選中),然后檢查結果的長度是否不為零:
if (!$('#chkBoxView:checked,#chkBoxEdit:checked,#chkBoxAdd:checked,#chkBoxDelete:checked').length) {
alert('Please select at least one checkBox');
return false;
} else {
// At least One Check Box Selected By User.
return true;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.