我有一个带复选框的表单。我想检查是否选中了特定的复选框。 我这样试试:
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>
所以那是错的。因为如果仅选中该复选框,则不必触发验证。
答案 0 :(得分:1)
这是适合我的代码。试一试。
if ($('#contactpersonen_canseestock_2 input:checked').val() == "1")
答案 1 :(得分:0)
你的答案中有很多代码,所以很难确切地说出你想要做什么。 但这就是我一直这样做的方式:
<input id="active_box" type="checkbox" />
if ($('#active_box').is(':checked')) {
// logic
}