[英]php/jQuery - formvalidation, get value of dynamically added inputs
[英]Jquery formvalidation required field if value is 0
我目前在頁面上插入了jQuery表單驗證,目前在嘗試驗證狀態下拉列表時遇到問題,視圖的構建方式是這樣的。 用戶選擇國家/地區,然后將其發回控制器,並返回鏈接到該國家/地區的狀態並建立狀態下拉列表。 在選擇國家之前,州下拉菜單的選定值為0,根據我的理解,該文本為“請選擇州”。jQuery表單驗證將看到0並采用其值,在我的情況下,這不是值我需要用戶選擇一個較大的值0。因此,當用戶按下Submit時,如果狀態選擇的值為0,則我需要顯示錯誤消息,否則請繼續。 這是我目前所擁有的
$('#myForm').validate({ // initialize the plugin
rules: {
"UserDetails.SelectedCountry": "required",
"UserDetails.SelectedState": {
required: function (element) {
var value = $("#UserDetails_SelectedState").val() == 0;
if (value) { // If the value is true I need to return false to make it required
alert("here");
return false;
} else { // Otherwise value is greater then 0 ok to carry on,
return true;
}
}
}
"UserDetails.Postcode": "required"
},
messages: {
"UserDetails.SelectedCountry": "Please choose your country",
"UserDetails.SelectedState": "Please choose your state",
"UserDetails.Postcode": "Please enter your postcode",
}
});
請問有人能夠告訴/顯示如何針對0進行驗證。
**更新**
我一直在努力地解決這個問題,正如您從我的jquery標記中看到的,如果它為true,然后返回false,我會看到警報消息“ here”,但該字段未突出顯示為紅色,錯誤消息也不為“請選擇顯示的“您的狀態”,那頭頂部的某人可能會頭疼嗎?
您可以將“請選擇狀態”值設為=“,然后將其禁用。
像這樣
<select>
<option value="" disabled>Please Choose a State</option>
<option value="1">1</option>
</select>
將您的驗證更改為此:
var value = $("#UserDetails_SelectedState").val()
if (value ==0 || value=="0") { // If the value is true I need to return false to make it required
alert("here");
return false;
} else { // Otherwise value is greater then 0 ok to carry on,
return true;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.