场景
前端向SSM后台进行提交时会提交多个对象,所以提交的是一个对象的List。所以页面上的names属性也是不确定的就比如这样:
<input type="text" name="bookInfoList[0].mobile" data-tip=" " data-valid="isNonEmpty||isMobile" data-error=" || " class="required w_60 ml_5 fl h_36em line_h_36em c_gray_777 bor_bottom bor_title_tint" value="" placeholder="手机号" />
然后是点击新增按钮时会自动追加元素。
比如这里有两个对象
name1为:
name2为:
但是可以知道的是页面最多提交5个对象。
实现
function checkMobile(){
var mobileCount=0;
if($("[name='bookInfoList[0].mobile']").val()!=undefined){
mobileCount++;
}
if($("[name='bookInfoList[1].mobile']").val()!=undefined){
mobileCount++;
}
if($("[name='bookInfoList[2].mobile']").val()!=undefined){
mobileCount++;
}
if($("[name='bookInfoList[3].mobile']").val()!=undefined){
mobileCount++;
}
if($("[name='bookInfoList[4].mobile']").val()!=undefined){
mobileCount++;
}
var mobileArray = new Array();
for(var i=0;i<mobileCount;i++){
mobileArray[i] = $("[name='bookInfoList["+i+"].mobile']").val()
}
if((new Set(mobileArray)).size != mobileArray.length){
//说明有重复元素
$ak.alert("所有乘客手机号不能重复", {
title: "信息提示",//弹窗标题
button_ok: "确定",
button_cancel: "取消",
icon: "error", //图标类型(warning,error,info,question,success)
animateIn: "bounceInDown",//弹窗显示效果
animateOut: "bounceOutUp"
});
return false;
}else{
return true;
}
}