复选框与文本框传值问题
复选框模式如下(动态循环生成,可能有多个,也可能一个都没有):
<td> <input type= "checkbox " value= "A " name= "seName "/> </td>
<td> <input type= "checkbox " value= "B " name= "seName "/> </td>
<td> <input type= "checkbox " value= "C " name= "seName "/> </td>
........................................................
文本框模式如下:
<td> <textarea name= "wbnr " rows= "3 "> </textarea> </td>
需要实现如下操作:
选一个,向文本框中加入一个值,中间以逗号隔开(如:A,B,C);
不选,如已经累加,则自动减去(如:B不选,则上面变为:A,C);
望各位指教!
[解决办法]
Easy, L@_@K
<!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>
<title> dhtml.checkbox.dynamicShowResult.html </title>
<meta name= "generator " content= "editplus " />
<meta name= "author " content= "yixianggao@126.com " />
<meta name= "keywords " content= "javascript " />
<meta name= "description " content= "for javascript of csdn region " />
</head>
<body>
<table>
<tr>
<td> <input type= "checkbox " value= "A " name= "seName " /> A </td>
<td> <input type= "checkbox " value= "B " name= "seName " /> B </td>
<td> <input type= "checkbox " value= "C " name= "seName " /> C </td>
<td> <textarea id= "wbnr " rows= "3 "> </textarea> </td>
</tr>
</table>
<script type= "text/javascript ">
<!--
function getCheckBoxListByName(strName)
{
var colInput = document.getElementsByTagName( "input ");
var colChkList = new Object;
colChkList.length = 0;
for (var i=0; i <colInput.length; i++)
{
if (colInput[i].type.toLowerCase()== "checkbox "
&& colInput[i].name==strName)
{
colChkList[colChkList.length] = colInput[i];
colChkList.length += 1;
}
}
return colChkList;
}
var oResult = document.getElementById( "wbnr ");
var colChkList = getCheckBoxListByName( "seName ");
for (var i=0; i <colChkList.length; i++)
{
colChkList[i].onclick = function() {
oResult.value = " ";
var arrResult = new Array();
for (var j=0; j <colChkList.length; j++)
{
if (colChkList[j].checked)
{
arrResult[arrResult.length] = colChkList[j].value;
}
}
oResult.value = arrResult.join( ", ");
}
}
//-->
</script>
</body>
</html>
[解决办法]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN ">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME= "Generator " CONTENT= "EditPlus ">
<META NAME= "Author " CONTENT= " ">
<META NAME= "Keywords " CONTENT= " ">
<META NAME= "Description " CONTENT= " ">
</HEAD>
<BODY>
<form >
<input type= "checkbox " name= "chk " value= "A "> A <br>
<input type= "checkbox " name= "chk " value= "B "> B <br>
<input type= "checkbox " name= "chk " value= "C "> C <br>
<textarea id= "text " rows= "5 " cols= "10 "> </textarea>
</form>
<script language= "javascript ">
function fangfa()
{
var src=event.srcElement;
var value=src.value;
if (src.checked)
{
str+=value;
}else{
str=str.replace(value, " ");
}
var arr=str.split( " ");
var text=document.getElementById( "text ");
text.value=arr.join( ", ").toString();
}
var str=new String();
var tagarr=document.getElementsByTagName( "input ");
for (var i=0;i <tagarr.length ;i++ )
{
tagarr[i].attachEvent( "onclick ",fangfa);
}
</script>
</BODY>
</HTML>