例如下的選項中,我們要求帶*的是必選的,看圖:
看到上面網頁沒?業務辦理管道下的多選必須要選中一個,怎麼辦?如果一個都沒有選中,在表單提交的時候我們就要去提示錯誤訊息,,,。大家都知道jquery驗證input是非常簡單的,卻很少要去驗證多選或單選,而且還是一組組的驗證,是分了組的,並非頁面上的所有! !怎麼辦呢?
不用jquery用其他的,可以嗎?可以! ! !不過我們還是用jquery來實它。
先來看一下,沒有選取的效果,我們應該是這樣:
假設選取了一個,提示訊息馬上消失,如下圖:
其實這個可以用jsmap實現 也就是用js模擬map來做。以下的程式碼是公用的,以後遇到這種情況,直接把下面js程式碼拷入,做一下設定就可以實現了。
用下面的程式碼,你必須引入jquery驗證的js
程式碼如下:
首先,把下面的js程式碼放入你的js檔案裡,或是頁面中:
var BHC = [], BCC = [], BQC = [], IC = [];
var BAC = [];
var BUC = [];
var setting = [
{
key: "BHC",
value: BHC
},
{
key: "BAC",
value: BAC
}, { key: "BUC", value: BUC }, { key: "BCC", value: BCC }, { key: "BQC", value: BQC }, {key:"IC",value:IC}
];
return {
//通过数组名得到数组织
getArr: function (arr_name) {
for (var setting_i = 0, setting_len = setting.length; setting_i < setting_len; setting_i++) {
if (setting[setting_i].key == arr_name) {
return setting[setting_i].value;
}
}
},
//删除指定数组中的某一个元素
delArr: function (arr_name, elementValue) {
for (var delArr_i = 0, delArr_len = setting.length; delArr_i < delArr_len; delArr_i++) {
if (setting[delArr_i].key == arr_name) {
for (var arr_i = 0, arr_len = setting[delArr_i].value.length; arr_i < arr_len; arr_i++) {
if (setting[delArr_i].value[arr_i] == elementValue) {
setting[delArr_i].value.splice(arr_i, 1);
}
}
for (var pushArr_i = 0, pushArr_len = setting.length; pushArr_i < pushArr_len; pushArr_i ) {
}
}
}
}
} ());
/**
*The click event of the check box or radio is applied to the above map
* @author Zhuang Puxiang, University of Electronic Science and Technology of China
*/
function chk(event, arrName) {
if (event.checked == true) {
//Add an element to the array named arrName
MapArr.pushArr(arrName, event.value);
} else {
//Remove an element from the array named arrName
MapArr.delArr(arrName, event.value);
var checkInput = document.getElementById(arrName);
if (MapArr.getArr(arrName)[0] == 'undefined' || MapArr.getArr(arrName)[0] == null) {
checkInput.value = null; //Give him the value of this array. In this case, it has the effect of verification
} else {
} checkInput.value = MapArr.getArr(arrName)[0];
//How to make the prompt information appear after adding the value Disappear?
if (!$("#form1").valid()) return false; //Just to make this chapter expand and disappear, so don't run the submitted check, it will affect the display of the chapter
}
Two: Add an input after the group of titles of the multi-select or single-select you need to verify:
Copy Code
The code is as follows:
Copy code
The code is as follows: