javascript 如何防止select option中option的重复添加
HTMLcode<html><head></head><body><scriptlanguage="javascript">functionaddOption(obj,n...
HTML code <html> <head></head> <body>
<script language="javascript">
function addOption(obj,name,value,check)
{ var option = document.createElement("option");
option.text = name; option.value = value;
document.getElementById(obj).add(option);
var xx=document.getElementById(obj).options.length;
document.getElementById(obj).options[xx-1].selected=check; }
</script>
<select name="select1" id="select1">
<option value='1'>张三</option> <option value='2'>李四</option>
</select>
<input type="button" value="添加" onClick="addOption('select1','王五1','3',true)">
</body> </html>
javascript 如何防止select option中option的重复添加 展开
<script language="javascript">
function addOption(obj,name,value,check)
{ var option = document.createElement("option");
option.text = name; option.value = value;
document.getElementById(obj).add(option);
var xx=document.getElementById(obj).options.length;
document.getElementById(obj).options[xx-1].selected=check; }
</script>
<select name="select1" id="select1">
<option value='1'>张三</option> <option value='2'>李四</option>
</select>
<input type="button" value="添加" onClick="addOption('select1','王五1','3',true)">
</body> </html>
javascript 如何防止select option中option的重复添加 展开
2013-09-09
展开全部
自己加一个语句判断一下就行了....把script里面获取长度的放在第一行..这样好判断点
代码如下:
<html>
<head></head>
<body>
<script language="javascript">
function addOption(obj,name,value,check){
//获取长度...
var xx=document.getElementById(obj).options.length;
//循环查看是否存在这样的值...
for(var i=0;i<xx;i++){
if(document.getElementById(obj).options[i].text==name && document.getElementById
(obj).options[i].value==value){
//如果存在则retuanfalse...程序就此停止..
return false;
}
}
// 如果不存在...则长度加1...
xx++;
//创建option..
var option = document.createElement("option");
//传递付值..
option.text = name;
option.value = value;
//添加
document.getElementById(obj).add(option);
//最后一个选中...
document.getElementById(obj).options[xx-1].selected=check;
}
</script>
<select name="select1" id="select1">
<option value='1'>张三</option> <option value='2'>李四</option>
</select>
<input type="button" value="添加" onClick="addOption('select1','王五1','3',true)">
</body> </html>
代码如下:
<html>
<head></head>
<body>
<script language="javascript">
function addOption(obj,name,value,check){
//获取长度...
var xx=document.getElementById(obj).options.length;
//循环查看是否存在这样的值...
for(var i=0;i<xx;i++){
if(document.getElementById(obj).options[i].text==name && document.getElementById
(obj).options[i].value==value){
//如果存在则retuanfalse...程序就此停止..
return false;
}
}
// 如果不存在...则长度加1...
xx++;
//创建option..
var option = document.createElement("option");
//传递付值..
option.text = name;
option.value = value;
//添加
document.getElementById(obj).add(option);
//最后一个选中...
document.getElementById(obj).options[xx-1].selected=check;
}
</script>
<select name="select1" id="select1">
<option value='1'>张三</option> <option value='2'>李四</option>
</select>
<input type="button" value="添加" onClick="addOption('select1','王五1','3',true)">
</body> </html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询