<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>book.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">--> <script type="text/javascript">
function gobook(){
var c = document.getElementById("bookList").getElementsByTagName("input");
var bookcar = document.getElementById("bookcar");
for(var i=0;i<c.length;i++){
if(c[i].checked){
var cName = c[i].nextSibling.firstChild.nodeValue;
var cValue = c[i].value;
var zhongNode = document.createElement("zhongNode");
var nameNode = document.createTextNode(cName);
zhongNode.value = cValue;
zhongNode.appendChild(nameNode);
bookcar.appendChild(zhongNode);
}
}
}
</script>
</head>
<body>
<div id="container">
<div id="bookList" name="bookList">
<div>
<input type="checkbox" id="shouji" name="shouji" value="shouji"><label for="shouji">手机</label>
</div>
<div>
<input type="checkbox" id="bld" name="bld" value="bld"><label for="bld">白兰地</label>
</div>
<div>
<input type="checkbox" id="lining" name="lining" value="lining"><label for="lining">李宁</label>
</div>
<div>
<input type="checkbox" id="ddc" name="ddc" value="ddc"><label for="ddc">电动车</label>
</div>
<div>
<input type="checkbox" id="mtc" name="mtc" value="mtc"><label for="mtc">摩托车</label>
</div>
<div>
<input type="submit" value="添加" onclick="gobook();">
</div>
<div>
<select id="bookcar" name="bookcar" multiple="multiple">
</select>
</div>
</div>
</div>
</body>
</html>
为什么不能把选中的添加进bookcar呢?
帮帮我哦
<html>
<head>
<title>book.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">--> <script type="text/javascript">
function gobook(){
var c = document.getElementById("bookList").getElementsByTagName("input");
var bookcar = document.getElementById("bookcar");
for(var i=0;i<c.length;i++){
if(c[i].checked){
var cName = c[i].nextSibling.firstChild.nodeValue;
var cValue = c[i].value;
var zhongNode = document.createElement("zhongNode");
var nameNode = document.createTextNode(cName);
zhongNode.value = cValue;
zhongNode.appendChild(nameNode);
bookcar.appendChild(zhongNode);
}
}
}
</script>
</head>
<body>
<div id="container">
<div id="bookList" name="bookList">
<div>
<input type="checkbox" id="shouji" name="shouji" value="shouji"><label for="shouji">手机</label>
</div>
<div>
<input type="checkbox" id="bld" name="bld" value="bld"><label for="bld">白兰地</label>
</div>
<div>
<input type="checkbox" id="lining" name="lining" value="lining"><label for="lining">李宁</label>
</div>
<div>
<input type="checkbox" id="ddc" name="ddc" value="ddc"><label for="ddc">电动车</label>
</div>
<div>
<input type="checkbox" id="mtc" name="mtc" value="mtc"><label for="mtc">摩托车</label>
</div>
<div>
<input type="submit" value="添加" onclick="gobook();">
</div>
<div>
<select id="bookcar" name="bookcar" multiple="multiple">
</select>
</div>
</div>
</div>
</body>
</html>
为什么不能把选中的添加进bookcar呢?
帮帮我哦
应该是
var zhongNode = document.createElement("option"); 哈哈,问题解决了
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>book.html </title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!-- <link rel="stylesheet" type="text/css" href="./styles.css">--> <script type="text/javascript">
function gobook(){
var c = document.getElementsByName("bookList");
var bookcar = document.getElementById("bookcar");
alert(bookcar.options.length);
for(var i=0;i <c.length;i++){
if(c[i].checked){
bookcar.options[bookcar.options.length]=new Option(c[i].value,c[i].value);
}
}
}
</script>
</head>
<body> <div id="container">
<div>
<input type="checkbox" name="bookList" value="手机">手机<br />
<input type="checkbox" name="bookList" value="白兰地">白兰地<br />
<input type="checkbox" name="bookList" value="李宁">李宁<br />
<input type="checkbox" name="bookList" value="电动车">电动车<br />
<input type="checkbox" name="bookList" value="摩托车">摩托车<br />
<input type="submit" value="添加" onClick="gobook();">
</div>
<div>
<select id="bookcar" name="bookcar" multiple="multiple">
</select>
</div>
</div> </div>
</body>
</html>
function gobook() {
var objSelect = document.getElementById("bookcar");
objSelect.options.length = 0;
var c = document.getElementById("bookList").getElementsByTagName("input");
var bookcar = document.getElementById("bookcar");
for (var i = 0; i < c.length; i++) {
if (c[i].checked) {
var cName = c[i].nextSibling.firstChild.nodeValue;
var cValue = c[i].value;
var zhongNode = document.createElement("option");
var nameNode = document.createTextNode(cName);
zhongNode.value = cValue;
zhongNode.appendChild(nameNode);
bookcar.options.appendChild(zhongNode);
}
}
}
<div id="container">
<div id="bookList" name="bookList">
<div>
<input type="checkbox" id="shouji" name="shouji" value="shouji"><label for="shouji">手机 </label>
</div>
<div>
<input type="checkbox" id="bld" name="bld" value="bld"><label for="bld">白兰地 </label>
</div>
<div>
<input type="checkbox" id="lining" name="lining" value="lining"><label for="lining">李宁 </label>
</div>
<div>
<input type="checkbox" id="ddc" name="ddc" value="ddc"><label for="ddc">电动车 </label>
</div>
<div>
<input type="checkbox" id="mtc" name="mtc" value="mtc"><label for="mtc">摩托车 </label>
</div>
<div>
<input type="button" value="添加" onclick="gobook();">
</div>
<div>
<select id="bookcar" name="bookcar" multiple="multiple"> </select>
</div>
</div> </div> 楼主注意了"><label 这个 >< 之间不能有空格
具体的看一下:http://www.cnblogs.com/lonelystar/archive/2008/08/22/1274161.html