如何在JQuery中使用if条件自定义消息规则?
我想添加一条带有条件的JQuery规则消息,条件x的意思是show me如何在JQuery中使用if条件自定义消息规则?,jquery,jquery-ui,Jquery,Jquery Ui,我想添加一条带有条件的JQuery规则消息,条件x的意思是show memessageX,如果是y,则show memessageY 这是我的密码: addRuleAmount: function($element) { $element.rules("add", { ReqAmount : true, // function validationAmount : true, // function mess
messageX
,如果是y,则show memessageY
这是我的密码:
addRuleAmount: function($element) {
$element.rules("add", {
ReqAmount : true, // function
validationAmount : true, // function
messages: {
ReqAmount : "Required Amount !!",
validationAmount : ($("#product_Type option:selected").attr('id') ==='car' ? "the amount must be between -100 and 100." : "the amount must be between 0 and 100.")
}
});
},
如果使用一些静态关联,可以考虑使用对象
var messages = {
car: "The amount must be between -100 and 100.",
box: "The amount must be between 0 and 100.",
wrench: "The amount must be between 0 and 1."
};
这为您提供了一种更具关联性的方法。所以你可以这样称呼它:
addRuleAmount: function($element) {
$element.rules("add", {
ReqAmount : true, // function
validationAmount : true, // function
messages: {
ReqAmount : "Required Amount !!",
validationAmount : messages[$("#product_Type option:selected").attr("id")]
}
});
}
这似乎不是一个完整的例子。我建议在定义规则之前添加条件。这里有什么问题?