我有一個帶有複選框的表單。我想檢查一個特定的複選框是否被選中。 我嘗試這樣的:如何檢查表單上的複選框是否被選中?
jQuery.validator.addMethod("checkDuplicate",function(value) {
//var user_id = $(this).closest("form").attr('id');
var id = $('#my_form').closest('form').attr('id');
console.log(id);
var ka = $('#ID').val();
if ($("#contactpersonen_canseestock_2").is(':checked') && $(#my_form':checkbox:checked').length == 1) {
console.log("checkbox checked; dont check duplicate email");
return true;
}
else{
console.log("checkbox not checked; check duplicate email");
if ($("#contactpersonen_canseestock_2").is(":checked"))
{
console.log('hoi');
}
console.log(ka);
return false;
}
}, "Email adres bestaat al");
jQuery.validator.classRuleSettings.checkTotal = { checkTotal: true };
,這是HTML:
<div class="contact-label span2">
<label for="contactpersonen-email">Email adres</label>
<div class="contact-input-field">
<input type="text" class="input-text span2 contactpersonen-email1 required checkDuplicate" id="contactpersonen_email1" name="contactpersonen_email1"></input>
</div>
</div>
但每次只去了別的
時間,它是一個手風琴。所以每次如果添加聯繫人的ID改爲:
<div class="contact-label span11 rights" id="formid">
<div class="row-fluid">
<div class="span3">
<input type="checkbox" class="input-text" value="Y" name="contactpersonen_canorder_0" id="contactpersonen_canorder_0"></input>
<label class="checkbox" for="contactpersonen_canorder_0">Mag border plaatsen</label>
<input type="checkbox" class="required checkTotal" value="Y" name="contactpersonen_canseestock_0" id="contactpersonen_canseestock_2"></input>
<label class="checkbox" for="contactpersonen_canseestock_0">Mag goederen afhalen</label>
</div>
<div class="span3">
<input type="checkbox" class="input-text" value="Y" name="contactpersonen_canseeorders_0" id="contactpersonen_canseeorders_0"></input>
<label class="checkbox" for="contactpersonen_canseeorders_0">Mag orders inzien</label>
<input type="checkbox" class="input-text" value="Y" name="contactpersonen_canaddaddress_0" id="contactpersonen_canaddaddress_0"></input>
<label class="checkbox" for="contactpersonen_canaddaddress_0">Mag bezorgadressen aanpassen</label>
</div>
<div class="span3">
<input type="checkbox" class="input-text" value="Y" name="contactpersonen_canseeprice_0" id="contactpersonen_canseeprice_0"></input>
<label class="checkbox" for="contactpersonen_canseeprice_0">Mag netto prijs zien</label>
<input type="checkbox" class="input-text" value="Y" name="contactpersonen_canseecredit_0" id="contactpersonen_canseecredit_0"></input>
<label class="checkbox" for="contactpersonen_canseecredit_0">Mag facturen en creditfacturen inzien</label>
</div>
<div class="span3">
<input type="checkbox" class="input-text" value="Y" name="contactpersonen_canseepickup_0" id="contactpersonen_canseepickup_0"></input>
<label class="checkbox" for="contactpersonen_canseepickup_0">Mag wijzigingen doorgeven</label>
</div>
</div>
</div>
奧凱,我現在有這樣的:
jQuery.validator.addMethod("checkDuplicate",function(value) {
//var user_id = $(this).closest("form").attr('id');
var id = $('#my_form').closest('form').attr('id');
var hallo = $('form').attr('id');
console.log(hallo);
console.log(id);
var ka = $('#ID').val();
if ($('#contactpersonen_canseestock_2 input:checked').val() == "1") {
console.log("checkbox checked; dont check duplicate email");
return true;
}
else{
console.log("checkbox not checked; check duplicate email");
//if ($("#contactpersonen_canseestock_2").is(":checked"))
if($('#my_form:checkbox:checked').length == 1)
{
console.log('hoi');
}
console.log(ka);
return false;
}
}, "Email adres bestaat al");
jQuery.validator.classRuleSettings.checkTotal = { checkTotal: true };
<div class="contact-label span2">
<label for="contactpersonen-email">Email adres</label>
<div class="contact-input-field">
<input type="text" class="input-text span2 contactpersonen_email required checkDuplicate" id="contactpersonen_email" name="contactpersonen_email"></input>
</div>
</div>
,但仍然只去在別的
<div class="contact-label span11 rights" id="formid">
<div class="row-fluid">
<div class="span3">
<input type="checkbox" class="input-text" value="Y" name="contactpersonen_canorder_0" id="contactpersonen_canorder_0"></input>
<label class="checkbox" for="contactpersonen_canorder_0">Mag order plaatsen</label>
<input type="checkbox" class="required checkTotal" value="Y" name="contactpersonen_canseestock_0" id="contactpersonen_canseestock_2"></input>
<label class="checkbox" for="contactpersonen_canseestock_0">Mag goederen afhalen</label>
</div>
<div class="span3">
<input type="checkbox" class="input-text" value="Y" name="contactpersonen_canseeorders_0" id="contactpersonen_canseeorders_0"></input>
<label class="checkbox" for="contactpersonen_canseeorders_0">Mag orders inzien</label>
<input type="checkbox" class="input-text" value="Y" name="contactpersonen_canaddaddress_0" id="contactpersonen_canaddaddress_0"></input>
<label class="checkbox" for="contactpersonen_canaddaddress_0">Mag bezorgadressen aanpassen</label>
</div>
<div class="span3">
<input type="checkbox" class="input-text" value="Y" name="contactpersonen_canseeprice_0" id="contactpersonen_canseeprice_0"></input>
<label class="checkbox" for="contactpersonen_canseeprice_0">Mag netto prijs zien</label>
<input type="checkbox" class="input-text" value="Y" name="contactpersonen_canseecredit_0" id="contactpersonen_canseecredit_0"></input>
<label class="checkbox" for="contactpersonen_canseecredit_0">Mag facturen en creditfacturen inzien</label>
</div>
<div class="span3">
<input type="checkbox" class="input-text" value="Y" name="contactpersonen_canseepickup_0" id="contactpersonen_canseepickup_0"></input>
<label class="checkbox" for="contactpersonen_canseepickup_0">Mag wijzigingen doorgeven</label>
</div>
</div>
</div>
</div>
所以這是錯誤的。因爲如果僅選中該複選框,則不會觸發驗證。
我看到你的例子中,沒有元素具有contactpersonen_canseestock_2'的'的ID。你也在'$(#my_form'中有拼寫錯誤:複選框:已勾選')' – j08691
我更新了帖子 – SavantCode
仍然有錯誤,請再次檢查 – Dekel