Ajax如何传输数组

在AJAX传输数值时,有时根据需求要传输一组数组给服务端,例如如果我们要对一组表单进行级联删除,首先我们需要做的就是提取选中项,然后将其取出并组成一个数组,待获得所有选中项后得到一个完整数组,然后我们将这个数组转换为字符串从而进行赋值传送,代码如下:

function delItem(form) {
if(confirm(’真的要删除吗?’)) {
var a = new Array();   //新建一个数组
for (var i=0;i<document.form1.elements.length;i++) {//获得表单input项
var e = document.form1.elements[i];//获取具体表单
if (e.checked==true && e.type==’checkbox’) {//判断表单是否未选中表单
a.push(e.value);//如果符合以上条件,将其值插入数组
}
}
if (a.length==0) {
alert(’没有选择!’);
return false;
}
$.get(’?action=delete&ids=’+a.join(’,'),null,function (msg) {//使用a.join将数组装换为字符串,并将其值赋予变量ids
alert(msg);
}
});
}
}

相关文章: 

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

还没有评论。

发表评论

(必填)

(必填)