需求: 这是一个Repeater循环出来的,不允许头衔名重复。
思路:给TextBox框添加onBlur事件,在JS中获取到TextBox中的值,进行循环判断。
做法:1.aspx中代码:Repeater中的TextBox控件: 这里主要用到NM
<asp:TextBox ID="txtHonor" nm="txtLevelName" runat="server" MaxLength="25" οnblur="checkLevelName() "> </asp:TextBox>
2.JS中代码。注意进入Jquery1.7.1-min
<script type="text/javascript">
function checkLevelName() {
$.each($("input[nm='txtLevelName']"), function (i, item) { 获取nm="txtLevelName" 的控件并进行循环 ,其中i表示索引,item表示获得的控件
if (item.value != "") { 获取到的控件中输入值了
$.each($("input[nm='txtLevelName']"), function (j, jtem) { 再次循环控件
if (i != j) { 两次比较的不是同一索引的控件,
if (item.value == jtem.value) { 值相等,Message
alert("不能输入重复头衔名!");
}
}
});
}
});
}
</script>