用js实现把一组同名复选框中已经选中的复选框的value值都加入到一个树组中!
用js实现把一组同名复选框中已经选中的复选框的value值都加入到一个树组中!
------解决方案-------------------- <html>
<head>
<title> Test </title>
</head>
<body>
<input type= "checkbox " name= "checkBoxName " value= "check1 " onclick= "javascript:showValue() "> <br>
<input type= "checkbox " name= "checkBoxName " value= "check2 " onclick= "javascript:showValue() "> <br>
<input type= "checkbox " name= "checkBoxName " value= "check3 " onclick= "javascript:showValue() "> <br>
<input type= "checkbox " name= "checkBoxName " value= "check4 " onclick= "javascript:showValue() "> <br>
<input type= "text " id= "textField " value= " "> <br>
<script>
function showValue(){
var a = new Array();
var checkBoxes = document.getElementsByName( "checkBoxName ")
var idx=0;
for(var i=0; i <checkBoxes.length; i++){
if(checkBoxes[i].checked == true)
a[idx++] = checkBoxes[i].value;
}
textField.value = a;
}
</script>
</body>
</html>
------解决方案-------------------- <html>
<head> <title> CheckBox Selected By Ctrl </title> </head>
<script>
function changeRadio(obj){
var size=0;
var checkObj = document.getElementsByName( "check ");
for(i=0;i <checkObj.length;i++){
if(checkObj[i].checked){
size++;
}
}
var valueArray = new Array(size);
var cnt = 0;
for(j=0;j <checkObj.length;j++){
if(checkObj[j].checked)
valueArray[cnt++]=checkObj[j].value;
}
for(k=0;k <valueArray.length;k++){
targetObj.value+=valueArray[k]+ ", ";
}
}
/**
* 给隐藏域的name赋值,便于name组装成一个数组
* @checkIndex 选择的checkBox对象所在的索引下标
* @dataValue 需要对目录对象赋值的值
*/
function datanameEnable(checkIndex,dataValue) {
var target = event.target || event.srcElement;
var checkBoxList = document.getElementsByName(target.name);
var chk = checkBoxList[checkIndex];
if(event.ctrlKey){
var checki=-1;
if(chk.checked){
for(i=checkIndex-1;i> =0;i--){
if(checkBoxList[i].checked && checkIndex!=i){
checki=i;
break;
}
}
if(checki> -1){
if(checkIndex> =checki){
for(j=(checki+1);j <checkIndex;j++){
checkBoxList[j].checked=!checkBoxList[j].checked;;
}
}
}
}else{
var unchecki = -1;
for(k=checkIndex-1;k> =0;k--){
if(!checkBoxList[k].checked && checkIndex!=k){
unchecki=k;
break;
}
}
if(unchecki> -1 && checkIndex> =unchecki){
for(j=unchecki+1;j <checkIndex;j++){
checkBoxList[j].checked=!checkBoxList[j].checked;;
}
}
}
}
}
</script>
<body>
<input type= "checkBox " name= "check " value= "check1 " onclick= "javascript:datanameEnable(0, 'check1 '); "> 记录1 <br>
<input type= "checkBox " name= "check " value= "check2 " onclick= "jav