var i = 0, j = 0;
for (i=0; i<form1.select1.options.length; i++) {
if( form1.select1.options(i).selected == true){
var add_ok = true;
for (j=0; j<form1.select2.options.length; j++) {
if (form1.select2.options(j).value == form1.select1.options(i).value){
alert(form1.select2.options(j).text +" 已经存在!" );
add_ok = false;
break;
};
};
if (add_ok){
var oOption = document.createElement("OPTION");
oOption.text=form1.select1.options(i).text;
oOption.value=form1.select1.options(i).value;
form1.select2.add(oOption);
form1.select1.remove(i);
i=i-1;
};
};
};求大神帮帮忙转换成jquery,谢谢啦JavaScriptjQuery
for (i=0; i<form1.select1.options.length; i++) {
if( form1.select1.options(i).selected == true){
var add_ok = true;
for (j=0; j<form1.select2.options.length; j++) {
if (form1.select2.options(j).value == form1.select1.options(i).value){
alert(form1.select2.options(j).text +" 已经存在!" );
add_ok = false;
break;
};
};
if (add_ok){
var oOption = document.createElement("OPTION");
oOption.text=form1.select1.options(i).text;
oOption.value=form1.select1.options(i).value;
form1.select2.add(oOption);
form1.select1.remove(i);
i=i-1;
};
};
};求大神帮帮忙转换成jquery,谢谢啦JavaScriptjQuery
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<title>无标题文档</title>
</head><body>
<!--<div id="msg"></div>-->
<form name="form1" id="form1">
<select name="select1" id="select1">
<option value="1">张三</option>
<option value="2">李四</option>
<option value="3">王五</option>
</select><input type="button" value="add" onclick="add()" />
<select name="select2" id="select2">
<option value="3">王五</option>
</select>
</form>
<script type="text/javascript">
function add(){
var i = 0, j = 0;
for (i=0; i<form1.select1.options.length; i++) {
if(form1.select1.options[i].selected == true){
var add_ok = true;
for (j=0; j<form1.select2.options.length; j++) {
if(form1.select2.options[j].value == form1.select1.options[i].value){
alert(form1.select2.options[j].text +" 已经存在!" );
add_ok = false;
break;
}
}
if(add_ok){
var oOption = document.createElement("OPTION");
oOption.text=form1.select1.options[i].text;
oOption.value=form1.select1.options[i].value;
form1.select2.add(oOption);
form1.select1.remove[i];
i=i-1;
}
}
}
}
</script>
</body>
</html>
是这么个东东吗
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<title>无标题文档</title>
</head><body>
<!--<div id="msg"></div>-->
<form name="form1" id="form1">
<select name="select1" id="select1">
<option value="1">张三</option>
<option value="2">李四</option>
<option value="3">王五</option>
</select><input type="button" value="add" onclick="add()" />
<select name="select2" id="select2">
<option value="3">王五</option>
</select>
</form>
<script type="text/javascript">
/*function add(){
var i = 0, j = 0;
for (i=0; i<form1.select1.options.length; i++) {
if(form1.select1.options[i].selected == true){
var add_ok = true;
for (j=0; j<form1.select2.options.length; j++) {
if(form1.select2.options[j].value == form1.select1.options[i].value){
alert(form1.select2.options[j].text +" 已经存在!" );
add_ok = false;
break;
}
}
if(add_ok){
var oOption = document.createElement("OPTION");
oOption.text=form1.select1.options[i].text;
oOption.value=form1.select1.options[i].value;
form1.select2.add(oOption);
form1.select1.remove[i];
i=i-1;
}
}
}
}*/
function add(){
//找到select1选中的option
var s=$('#select1 option:selected').val();
//是否已经存在于select2
var isexist=$('#select2').find('option[value='+s+']').length;
if( isexist==0)
$('#select2').append($('#select1 option:selected'));
}
jQuery(function($){
/*var user = {"name":"张三","age":14,"wife":null}; var chm="姓名:"+user.name+"<br>"
+"年龄:"+user.age+"<br>"
+"配偶:"+user.wife;
$('#msg').append(chm);*/
});
</script>
</body>
</html>