动态添加的多个select,怎么判断选择值
动态添加的多个select,如何判断选择值
<script language="javascript">
function addTxtProductNameBox()
{ var oTb = document.getElementById("TableProductName");
var oTr = oTb.insertRow();
var newTd0 = oTr.insertCell();
var newTd1 = oTr.insertCell();
var newTd2 = oTr.insertCell();
var newTd3 = oTr.insertCell();
var newTd4 = oTr.insertCell();
var newTd5 = oTr.insertCell();
newTd0.innerHTML ="材料<select id='select_cl' name='select_cl'><option value=''>请选择材料</option><option value='单模FC/PC跳线(3-5M)'>单模FC/PC跳线(3-5M)</option><option value='单模FC/APC跳线(3-5M)'>单模FC/APC跳线(3-5M)</option><option value='单模SC/PC跳线(3-5M)'>单模SC/PC跳线(3-5M)</option><option value='单模SC/APC跳线(3-5M)'>单模SC/APC跳线(3-5M)</option><option value='单模FC/PC跳线(10M)'>单模FC/PC跳线(10M)</option</select>";
}
function returncheck()
{
}</script>
<form id="form_add" name="form_add" method="post" action="?action=add">
<table width="100%" border="0" cellpadding="1" cellspacing="1" bgcolor="#9900FF" id="TableProductName">
<tr>
<td width="20%" height="24" bgcolor="#CCCCFF"><label>
<input name="Sub_add" type="button" id="Sub_add" onClick="addTxtProductNameBox()" value="添加材料" />
</label>
<label>
<label></label>
<label></label></td>
<td width="11%" bgcolor="#CCCCFF"><label>
<input name="Sub_save" type="submit" id="Sub_save" value="保存" onClick="return returncheck();" />
</label>
<label></label></td>
<td width="7%" bgcolor="#CCCCFF"> </td>
<td width="21%" bgcolor="#CCCCFF"><input name="Sub_rest" type="reset" id="Sub_rest" value="重置数据" onClick="reasetform()"/>
<input name="hidd_anjia_id" type="hidden" id="hidd_anjia_id" /></td>
<td width="20%" bgcolor="#CCCCFF"> </td>
<td width="21%" bgcolor="#CCCCFF"> </td>
</tr>
<tr>
<td height="24" colspan="6" bgcolor="#CCCCFF">说明:材料只能</td>
</tr>
</table>
</form>
如果添加多个select ,在returncheck()中如何判断select的值为空,和重复,如果就出alert提示??
------解决方案--------------------
我还是使用jquery来帮你操作吧,建议多接触一下jquery,繁琐的代码没必要,
function returncheck()
{
var flag = true;
//获取多个select,当然$("select")这样写是获取当前页面所有的select
$("select").each(function(){
当发现有空值的就终止循环,并将flag设置为false;避免全数组检查,浪费时间
if(!$(this).val()){
flag = false;
return false;//return false是为了终止循环,相当于break;
}
});
return flag;
}
------解决方案--------------------
$(#select_cl).val() jquery方法 ,所有选中的值返回 数组。
js写法:
var s=document.all['select_cl'].options; 返回数组,是dom对象
遍历
for(var i=0;i<s.length;i++){
if(s[i].checked==true)
alert(s[i].value);
}
------解决方案--------------------
<script language="javascript">
function addTxtProductNameBox()
{ var oTb = document.getElementById("TableProductName");
var oTr = oTb.insertRow();
var newTd0 = oTr.insertCell();
var newTd1 = oTr.insertCell();
var newTd2 = oTr.insertCell();
var newTd3 = oTr.insertCell();
var newTd4 = oTr.insertCell();
var newTd5 = oTr.insertCell();
newTd0.innerHTML ="材料<select id='select_cl' name='select_cl'><option value=''>请选择材料</option><option value='单模FC/PC跳线(3-5M)'>单模FC/PC跳线(3-5M)</option><option value='单模FC/APC跳线(3-5M)'>单模FC/APC跳线(3-5M)</option><option value='单模SC/PC跳线(3-5M)'>单模SC/PC跳线(3-5M)</option><option value='单模SC/APC跳线(3-5M)'>单模SC/APC跳线(3-5M)</option><option value='单模FC/PC跳线(10M)'>单模FC/PC跳线(10M)</option</select>";
}
function returncheck()
{
}</script>
<form id="form_add" name="form_add" method="post" action="?action=add">
<table width="100%" border="0" cellpadding="1" cellspacing="1" bgcolor="#9900FF" id="TableProductName">
<tr>
<td width="20%" height="24" bgcolor="#CCCCFF"><label>
<input name="Sub_add" type="button" id="Sub_add" onClick="addTxtProductNameBox()" value="添加材料" />
</label>
<label>
<label></label>
<label></label></td>
<td width="11%" bgcolor="#CCCCFF"><label>
<input name="Sub_save" type="submit" id="Sub_save" value="保存" onClick="return returncheck();" />
</label>
<label></label></td>
<td width="7%" bgcolor="#CCCCFF"> </td>
<td width="21%" bgcolor="#CCCCFF"><input name="Sub_rest" type="reset" id="Sub_rest" value="重置数据" onClick="reasetform()"/>
<input name="hidd_anjia_id" type="hidden" id="hidd_anjia_id" /></td>
<td width="20%" bgcolor="#CCCCFF"> </td>
<td width="21%" bgcolor="#CCCCFF"> </td>
</tr>
<tr>
<td height="24" colspan="6" bgcolor="#CCCCFF">说明:材料只能</td>
</tr>
</table>
</form>
如果添加多个select ,在returncheck()中如何判断select的值为空,和重复,如果就出alert提示??
------解决方案--------------------
我还是使用jquery来帮你操作吧,建议多接触一下jquery,繁琐的代码没必要,
function returncheck()
{
var flag = true;
//获取多个select,当然$("select")这样写是获取当前页面所有的select
$("select").each(function(){
当发现有空值的就终止循环,并将flag设置为false;避免全数组检查,浪费时间
if(!$(this).val()){
flag = false;
return false;//return false是为了终止循环,相当于break;
}
});
return flag;
}
------解决方案--------------------
$(#select_cl).val() jquery方法 ,所有选中的值返回 数组。
js写法:
var s=document.all['select_cl'].options; 返回数组,是dom对象
遍历
for(var i=0;i<s.length;i++){
if(s[i].checked==true)
alert(s[i].value);
}
------解决方案--------------------
<input name="Sub_save" type="submit" id="Sub_save" value="保存" onClick="returncheck.call(this);" />
function returncheck(){
//javascript
var tr = this.parentElement.parentElement;
var sels = document.getElementsByName("select_cl");
for(var i = 0 ; i < sels.length ; i++){
if(tr === sels[i].parentElement.parentElement){