在JavaScript中传递数组HTML元素
Pass Array HTML Element(s) in JavaScript
我有一个像下面这样的复选框数组,
<input type="checkbox" value="1" id="a_r_id[1]" name="a_r_id[1]">
<input type="checkbox" value="1" id="a_r_id[2]" name="a_r_id[2]">
<input type="checkbox" value="1" id="a_r_id[3]" name="a_r_id[3]">
<input type="checkbox" value="1" id="a_r_id[4]" name="a_r_id[4]">
在我的页面…我想通过JavaScript (jQuery AJAX)提交只检查一个…我该怎么做呢?
编辑:
实际上,我想得到所有的数组键上的复选框,以便我可以通过ajax发布它。像"1,4"这样的字符串
var keys = [],
keystring;
$('input[name^="a_r_id"]:checked').each(function () {
keys.push($(this).attr('name').replace(/a_r_id'[('d+)']/, '$1'));
});
keystring = keys.join();
当然,有更好的方法来做到这一点,但这回答了你的问题,因为你已经提出了。
我终于找到了上面问题的答案。我把它写在这里。
问题:我怎么能得到"键"从数组HTML元素(s) ?(在我的情况下,只选中复选框,我想得到)
我的答案代码是这样的:
//first, i get every checked checkbox using jQuery selector,
//as mentioned by DerekHenderson.
var list_agent = $('input[name^="a_r_id"]:checked');
var l_c_agent = new Array();
//then, i create a loop to loop each object returned.
for(var i=0;i<list_agent.length;i++){
//after that, i'm using Regular Expression ( match() ) on every returned object id and throw it into some array.
l_c_agent[i] = list_agent[i].id.match(/[0-9]+/);
}
//finally, i join the array using javascript join() method so that i can pass it using jQuery AJAX as a string to my controller and process it.
var clean_agent_list = l_c_agent.join();
var add_url = 'test.php';
$.ajax({
url: add_url,
type: "GET",
data : { 'list_agent' : clean_agent_list },
success: function(data_return) {
//alert(data_return);
}
});
输出将是这样的(如果使用上面的示例问题,并且我们只检查id为1,3和4的元素)
1,3,4
如果有人有更好的代码,请写在这里,以便我们讨论哪一个更好地解决我的问题
您想要的方法似乎有点落后;浏览器将只提交选中的复选框,但这里是:
var re = /'[('d+)']$/,
numbers = [];
$('input[name^="a_r_id''["]:checked').each(function() {
numbers.push(+this.name.match(re)[1]);
});
console.log(numbers.join(','));
它选择所有名称以"a_r_id["开头的复选框。然后,使用正则表达式提取方括号之间的数字部分并添加到值列表中。
我想你应该这样做
<input type="checkbox" value="1" id="a_r_id_1" name="a_r_id[]">
<input type="checkbox" value="2" id="a_r_id_2" name="a_r_id[]">
<input type="checkbox" value="3" id="a_r_id_3" name="a_r_id[]">
<input type="checkbox" value="4" id="a_r_id_4" name="a_r_id[]">
单选按钮似乎比复选框更适用。
<input type="radio" name="radiogroup" value="1" id="a_r_id[1]" name="a_r_id[1]">
<input type="radio" name="radiogroup" value="2" id="a_r_id[2]" name="a_r_id[2]">
<input type="radio" name="radiogroup" value="3" id="a_r_id[3]" name="a_r_id[3]">
您可以使用
获取所选值$("input:radio[name=radiogroup]").click(function() {
var value = $(this).val();
//
do something with var
//
});
相关文章:
- 使用Web Html表单创建Javascript数组
- 如何放置ÅÄ和#214;在javascript数组中,然后将其与html文本进行比较
- jQuery对象从html表中查询为两个一维数组,用于Chartist图表
- 正在转义javascript数组中的html字符
- 构建HTML选择字段并使用JavaScript数组选择选项
- 使用jQuery在html数组中设置一个值
- 如何在angular js/HTML中处理数组
- 如何将html中的列表项解析为javascript数组
- 如何查找给定数组中包含值的HTML元素
- 我想使用Javascript中的循环在HTML中的特定id中显示数组中的多个对象
- 如何使用angular指令从html中获取数组
- 如何将数组(Google应用程序脚本)返回到HTML侧边栏
- 每隔几秒钟从 html 中的数组中的值更改类
- 将 PHP 数组传递给 HTML 页面
- 在 HTML URL 中使用数组
- HTML 数组乘以 javascript
- 数组/HTML中的函数
- 选择多个复选框并将值分配给数组 - HTML/Javascript
- 在JavaScript中传递数组HTML元素
- 数组HTML Javascript中的图像