我想添加一个if语句来包围一些代码。我需要我的if语句(但在 jquery ):
If (chkSurety.checked = true){
SuretyFullName: { required: true }, // This is validation plugin and must stay like this.
SuretyIDNumber: { required: true},
SuretyPhysicalAddress: { required: true },
SuretyHomeTel: { required: true},
SuretyPlace: { required: true},
SuretyDay: { required: true },
SuretyMonth: { required: true }
}
Else
{
SuretyFullName: { required: false },
SuretyIDNumber: { required: true},
SuretyPhysicalAddress: { required: true },
SuretyHomeTel: { required: true},
SuretyPlace: { required: true},
SuretyDay: { required: true },
SuretyMonth: { required: true }
}
这个if语句是C#我需要它是jquery。这已经是一个事件,所以不希望checkbox.checked成为事件,只需在jquery中需要上面的代码。
提前致谢,如果您需要更多信息,请询问:)
答案 0 :(得分:3)
假设您的复选框的ID为chkSurety
,而您想要做的就是根据问题中给出的代码更新SuretyFullName
对象的状态:
SuretyFullName.required = $('#chkSurety').is(':checked');
或(更有效率)没有jQuery:
SuretyFullName.required = document.getElementById('chkSurety').checked;
出现您正在尝试为jQuery验证插件创建一些规则,因此:
// set the default rules
var rules = {
SuretyFullName: { required: false },
SuretyIDNumber: { required: true},
SuretyPhysicalAddress: { required: true },
SuretyHomeTel: { required: true},
SuretyPlace: { required: true},
SuretyDay: { required: true },
SuretyMonth: { required: true }
};
// override the full name rule, based on the checkbox
rules.SuretyFullName.required = $('#chkSurety').is(':checked');
$('#fmrMain').validate({
rules: rules,
...
});
答案 1 :(得分:2)
简单地说:
var yourObject = {SuretyFullName: { required: chkSurety.checked }};
如果您确实需要if
- 语法出于某种原因:
var yourObject = {}; if (chkSurety.checked) { yourObject.SuretyFullName = true; } else { yourObject.SuretyFullName = false; }
注意:if (chkSurety.checked == true)
是多余的,if (chkSurety.checked)
更好。
答案 2 :(得分:1)
试试这个:
if ($("#chkSurety").is(":checked")) {
// rest of code...
}
答案 3 :(得分:1)
如果你可以帮助它不包括整个JQuery库,如果它就像使用纯JavaScript一样容易
然而,因为它需要使用JQuery: - )
if ($('#chkSurety').is(":checked")) {
// checked
} else {
// not checked
}
http://jsfiddle.net/XXHrE/3/ - 测试一下......
答案 4 :(得分:0)
这很有效。
SuretyFullName: { required: function (element) { if ($('#chkSuretyShip').is(':checked')) { return true } else { return false} } }