简体   繁体   English

获取选中的复选框并存储在数组javascript中

[英]get checked check boxes and store in array javascript

anyone can tell me how can i check and add checkbox checked values into an array?? 任何人都可以告诉我如何检查复选框的值并将其添加到数组中? get checked values on click and store in an array in javascript. 在点击时获取检查值,并将其存储在javascript数组中。 This is my code. 这是我的代码。

function ajax(city)
{
var xmlhttp; 
try { 
    xmlhttp=new XMLHttpRequest(); 
} 
catch (e) { 
    try { 
        xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); 
    } 
      catch (e) { 
        try { 
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
        } 
        catch (e) { 
            alert("Your browser does not support AJAX!"); 
            return false; 
        } 
    } 
} 

var url='process.php?city='+city ;
xmlhttp.onreadystatechange=function() { 
    if(xmlhttp.readyState==4) 
        document.getElementById('search_city_projects').innerHTML = xmlhttp.responseText;
}
xmlhttp.open("GET",url,true); 
xmlhttp.send(null); 
}

Here is html code.. 这是html代码。

<input type="checkbox" name="checkbox[]" id="checkbox[]" value="<?php echo $cities[$i]->city; ?>" onclick="ajax('<?php echo $cities[$i]->city; ?>');" />

i don't how to add checkbox values that are gets from on click event and then store these values in array one by one in a single array and then pass these array's values to ajax call.Please help... Thanx in advance... 我不怎么添加来自click事件的复选框值,然后将这些值一个接一个地存储在数组中,然后将这些数组的值传递给ajax call.Please help ...提前感谢。 。

Do you mean: 你的意思是:


var chkedVals = new Array; //array to store checked checkboxes value
with(document.your_form_name) {
  for(var i = 0; i < checkbox.length; i++){
    if(checkbox[i].checked) {
       chkedVals.push(checkbox[i].value);
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM