我有兩個html元素。一個是選擇框,在它下面是一個文本輸入框。如果我更改上面的選擇框,那麼我想更改文本輸入框的JQuery驗證規則。下面的代碼似乎正確運行(標籤已更改),但驗證不會發生在焦點輸出或單擊事件上,就像我期望的那樣。修改規則後JQuery驗證不工作
這是我更改選擇框值時更改規則的代碼。我錯過了什麼嗎?
$('#Countries.selectpicker').on('change', function() {
$("#Location").val('');
var selected = $(this).find("option:selected").val();
if (selected != "0") {
//alert("other country selected");
$(".locationLabel").text("City");
$("#Location").attr("placeholder", "e.g. London");
$("#Location").attr("type", "string");
$("#Location").rules("remove");
$('#Location').rules("add", {
required: true,
minlength: 3,
maxlength: 40,
messages: {
required: "Required input",
minlength: jQuery.validator.format("Please, {0} characters are necessary"),
maxlength: jQuery.validator.format("Please, {0} characters are necessary")
}
});
} else {
//alert("us selected");
$(".locationLabel").text("Zip Code");
$("#Location").attr("placeholder", "e.g. 94901");
$("#Location").attr("type", "number");
$("#Location").rules("remove");
$('#Location').rules("add", {
required: true,
minlength: 5,
maxlength: 5,
messages: {
required: "Required input",
minlength: jQuery.validator.format("Please, {0} characters are necessary"),
maxlength: jQuery.validator.format("Please, {0} characters are necessary")
}
});
}
});
http://jsfiddle.net/arunpjohny/22recekc/1/? –
http://jsfiddle.net/arunpjohny/22recekc/2/ –
嘗試上面的鏈接爲什麼我發生選擇更改後出現錯誤,並嘗試輸入3個字符? http://jsfiddle.net/22recekc/3/ – user1186050