检查复选框并存储在数组javascript中 [英] get checked check boxes and store in array javascript

查看:49
本文介绍了检查复选框并存储在数组javascript中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以告诉我如何检查并将复选框选中的值添加到数组中?
在点击中获取选中的值并在javascript中存储在数组中。
这是我的代码。

  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(你的浏览器不支持AJAX!);
返回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);
}

这是html代码..

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

我不知道如何添加从点击事件中获取的复选框值,然后存储这些值在一个数组中逐个数组,然后将这些数组的值传递给ajax调用。请帮助...
提前使用Thanx ...

你是说:

 

var chkedVals = new Array ; //存储已检查复选框的数组值
with(document.your_form_name){
for(var i = 0; i< checkbox.length; i ++){
if(checkbox [i] .checked){
chkedVals.push(checkbox [i] .value);
}
}
}


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. 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..

<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...

解决方案

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);
    }
  }
}

这篇关于检查复选框并存储在数组javascript中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆