JS 动态增删元素 自定义验证控件的使用
function addRowContent(content)
{
var selector = document.getElementsByName("idcExitSelect");
if(!selector || selector.length < 5)
{
document.getElementById("idcExitTable").insertRow().insertCell().innerHTML = content;
}
}
function addRow()
{
content = "<SELECT name='idcExitSelect'>";
content += "<OPTION value='电信' selected>电信</OPTION>";
content += "<OPTION value='网通'>网通</OPTION>";
content += "<OPTION value='教育网'>教育网</OPTION>";
content += "<OPTION value='移动'>移动</OPTION>";
content += "<OPTION value='联通'>联通</OPTION>";
content += "</SELECT>";
var selector = document.getElementsByName("idcExitSelect");
if(!selector || selector.length < 5)
{
document.getElementById("idcExitTable").insertRow().insertCell().innerHTML = content;
}
}
function deleteRow()
{
var selector = document.getElementsByName("idcExitSelect");
if(selector.length > 1)
{
document.getElementById("idcExitTable").deleteRow(selector.length-2);
}
}
function checkRepeat(source,arguments)
{
var selector = document.getElementsByName("idcExitSelect");
for(var i = 0;i < selector.length;i++)
{
for(var j = 0;j < selector.length;j++)
{
if(i != j && selector[i].value == selector[j].value) //有重复的
{
//alert("不能选重复的机房出口");
selector[i].focus();
//return false;
arguments.IsValid=false;
return;
}
}
}
arguments.IsValid=true;
}
{
var selector = document.getElementsByName("idcExitSelect");
if(!selector || selector.length < 5)
{
document.getElementById("idcExitTable").insertRow().insertCell().innerHTML = content;
}
}
function addRow()
{
content = "<SELECT name='idcExitSelect'>";
content += "<OPTION value='电信' selected>电信</OPTION>";
content += "<OPTION value='网通'>网通</OPTION>";
content += "<OPTION value='教育网'>教育网</OPTION>";
content += "<OPTION value='移动'>移动</OPTION>";
content += "<OPTION value='联通'>联通</OPTION>";
content += "</SELECT>";
var selector = document.getElementsByName("idcExitSelect");
if(!selector || selector.length < 5)
{
document.getElementById("idcExitTable").insertRow().insertCell().innerHTML = content;
}
}
function deleteRow()
{
var selector = document.getElementsByName("idcExitSelect");
if(selector.length > 1)
{
document.getElementById("idcExitTable").deleteRow(selector.length-2);
}
}
function checkRepeat(source,arguments)
{
var selector = document.getElementsByName("idcExitSelect");
for(var i = 0;i < selector.length;i++)
{
for(var j = 0;j < selector.length;j++)
{
if(i != j && selector[i].value == selector[j].value) //有重复的
{
//alert("不能选重复的机房出口");
selector[i].focus();
//return false;
arguments.IsValid=false;
return;
}
}
}
arguments.IsValid=true;
}
定义CheckRepeat是因为需要验证机房出口不能重复。(在窗体中声明一个自定义验证控件)
<TR>
<TD class="Table-rlc" noWrap width="130"><SPAN class="txtTille1"><SPAN class="txtTille1"><SPAN class="txtTille1">机房出口:</SPAN></SPAN></SPAN></TD>
<TD class="Table-ic" noWrap colspan="3">
<SELECT name='idcExitSelect' id="idcExitSelectBase">
<OPTION value='电信' selected>电信</OPTION>
<OPTION value='网通'>网通</OPTION>
<OPTION value='教育网'>教育网</OPTION>
<OPTION value='移动'>移动</OPTION>
<OPTION value='联通'>联通</OPTION>
</SELECT>
<INPUT class="press" type="button" value="+" onclick="addRow()"> <INPUT class="press" type="button" value="-" onclick="deleteRow()">
<asp:customvalidator id="CustomValidator1" runat="server" ErrorMessage="不能选重复的机房出口" ClientValidationFunction="checkRepeat"> </asp:customvalidator>
<br>
<table id="idcExitTable" align="left" cellpadding="0" cellspacing="0" border="0">
</table>
</TD>
</TR>
<TD class="Table-rlc" noWrap width="130"><SPAN class="txtTille1"><SPAN class="txtTille1"><SPAN class="txtTille1">机房出口:</SPAN></SPAN></SPAN></TD>
<TD class="Table-ic" noWrap colspan="3">
<SELECT name='idcExitSelect' id="idcExitSelectBase">
<OPTION value='电信' selected>电信</OPTION>
<OPTION value='网通'>网通</OPTION>
<OPTION value='教育网'>教育网</OPTION>
<OPTION value='移动'>移动</OPTION>
<OPTION value='联通'>联通</OPTION>
</SELECT>
<INPUT class="press" type="button" value="+" onclick="addRow()"> <INPUT class="press" type="button" value="-" onclick="deleteRow()">
<asp:customvalidator id="CustomValidator1" runat="server" ErrorMessage="不能选重复的机房出口" ClientValidationFunction="checkRepeat"> </asp:customvalidator>
<br>
<table id="idcExitTable" align="left" cellpadding="0" cellspacing="0" border="0">
</table>
</TD>
</TR>
绑定的时候:
//输出机房出口
if(IdcObj.IdcExit.Trim().Length > 0)
{
InputExit(IdcObj.IdcExit);
}
if(IdcObj.IdcExit.Trim().Length > 0)
{
InputExit(IdcObj.IdcExit);
}
/// <summary>
/// 输出机房出口
/// </summary>
/// <param name="exitStr"></param>
private void InputExit(string exitStr)
{
string[] exitArray = exitStr.Split(',');
if(exitArray.Length >= 1)
{
string str = "<script>document.getElementById('idcExitSelectBase').value='{0}';</script>";
Page.RegisterStartupScript("ExitBaseJs",string.Format(str,exitArray[0]));
string js = "<script>";
for(int i = 1;i < exitArray.Length;i++)
{
string content = "<SELECT name='idcExitSelect' id='idcExitSelect" + i + "'>";
content += "<OPTION value='电信'>电信</OPTION>";
content += "<OPTION value='网通'>网通</OPTION>";
content += "<OPTION value='教育网'>教育网</OPTION>";
content += "<OPTION value='移动'>移动</OPTION>";
content += "<OPTION value='联通'>联通</OPTION>";
content += "</SELECT>";
js += "addRowContent(\" " + content + "\");document.getElementById('idcExitSelect" + i + "').value='" + exitArray[i] + "';\n";
}
js += "</script>";
Page.RegisterStartupScript("ExitJs",js);
}
}
/// 输出机房出口
/// </summary>
/// <param name="exitStr"></param>
private void InputExit(string exitStr)
{
string[] exitArray = exitStr.Split(',');
if(exitArray.Length >= 1)
{
string str = "<script>document.getElementById('idcExitSelectBase').value='{0}';</script>";
Page.RegisterStartupScript("ExitBaseJs",string.Format(str,exitArray[0]));
string js = "<script>";
for(int i = 1;i < exitArray.Length;i++)
{
string content = "<SELECT name='idcExitSelect' id='idcExitSelect" + i + "'>";
content += "<OPTION value='电信'>电信</OPTION>";
content += "<OPTION value='网通'>网通</OPTION>";
content += "<OPTION value='教育网'>教育网</OPTION>";
content += "<OPTION value='移动'>移动</OPTION>";
content += "<OPTION value='联通'>联通</OPTION>";
content += "</SELECT>";
js += "addRowContent(\" " + content + "\");document.getElementById('idcExitSelect" + i + "').value='" + exitArray[i] + "';\n";
}
js += "</script>";
Page.RegisterStartupScript("ExitJs",js);
}
}
要获取选上的出口:
idcObj.IdcExit = Request.Form["idcExitSelect"].ToString();