开发者

problem in retriving multiple checkbox values

I am unable to access the values of checkboxes i select. Here is my code which i tried.

<input type="checkbox" id="<%= "gname_" + in.intValue() %>" name="chkBox" onclick='chkboxSelect(localArrGroupNames[<%=in.intValue() %>],<%= "gname_" + in.intValue() %>)'>



JS code:  
function chkboxSelect( chkBoxValue, id){  
      var i=0;  
    if(id.checked)  {  
       var selected;  
       alert("id:  " + chkBoxValue + i);  
       selected[i] = chkBoxValue;  
       i++;  
    }  

    }  

I get only one value in my alert though I select more than one value. For example if开发者_开发百科 i select red, blue, green, After I select each, I get only one in my alert. Please help me . Thanks in advance.


If all checkboxes have the same name chkBox

<!DOCTYPE HTML>
<html>
<head>
<title>Title of the document</title>
<script>
  function getValues(){
        var cbs    = document.getElementsByName('chkBox');
        var result = '';

        for(var i=0; i<cbs.length; i++) {
         if(cbs[i].checked ) result += (result.length > 0 ? "," : "") + cbs[i].value;
        }

        alert(result);
        return result;
     }
</script>
</head>

<body>  
 <input type="checkbox" id="gname_1" name="chkBox" onclick='getValues();' value='red'>Red<br>
 <input type="checkbox" id="gname_2" name="chkBox" onclick='getValues();' value='green'>Green<br>
 <input type="checkbox" id="gname_3" name="chkBox" onclick='getValues();' value='blue'>Blue<br>
</body>

</html>


If you use multiple checkboxes with the same name, your code shoud be like this:

<input type="checkbox" name="chkBox[]" value="red">
<input type="checkbox" name="chkBox[]" value="green">
<input type="checkbox" name="chkBox[]" value="blue">

if you submit the form, you will get an array... ($_POST['chkBox'][0])

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜