[英]How to get the selected checkbox values on a button click?
How to get the selected checkbox values inside the list view on a button click? 如何在单击按钮的列表视图中获取所选复选框值?
I am building the list items dynamically using jQuery append. 我正在使用jQuery append动态构建列表项。
This is the jQuery code which creates the list items... The obj_address_list is an array of objects which each object will have name, alias, id and mobile. 这是创建列表项的jQuery代码... obj_address_list是一个对象数组,每个对象都有name,alias,id和mobile。 The value of the checkbox would be like 'Name' 复选框的值将类似于“名称”
$.each(obj_address_list, function(ctr, obj) {
$('#ul_address_list').append('<li data-icon="false">' +
'<a href="#" class="add-container">' +
'<label class="add-container" data-corners="false">' +
'<input name="chk_address" id="chk_address" type="checkbox" value="'+obj.name+'<'+obj.alias+'-'+obj.id+'>'+'" />' +
'<label class="lbl_add-container">' +
'<h3>'+obj.name+'</h3>' +
'<p>'+obj.mobile+'</p></div>' +
'</label>' +
'</label>' +
'</a>' +
'</li>');
});
$("input[name=chk_address]").checkboxradio();
$('#ul_address_list').listview('refresh');
Find below the screen after appending. 追加后在屏幕下方找到。
When the 'Send' button is clicked I need to capture all the selected checkbox values and pass it to an another page. 单击“发送”按钮时,我需要捕获所有选定的复选框值并将其传递给另一个页面。
Following is the code I am trying to use on tapping the send button. 以下是我在点击发送按钮时尝试使用的代码。 The id of the button is 'a_send-sms' and id of the checkbox is 'chk_address'. 按钮的id是'a_send-sms',复选框的id是'chk_address'。 But this doesnt seem to be working. 但这似乎并没有奏效。
$("#a_send-sms").on("tap",function(e){
e.preventDefault();
var selected = new Array();
$('#chk_address input:checked').each(function() {
alert('inside');
selected.push($(this).attr('value'));
});
console.log(selected);
});
What am I missing? 我错过了什么?
You cannot have multiple ID's, ID's must be unique. 您不能拥有多个ID,ID 必须是唯一的。 Give a class to those checkboxes and then iterate them: 给这些复选框一个类,然后迭代它们:
$('input.chk_address:checked')
^
So try: 所以尝试:
$("#a_send-sms").on("tap",function(e){
e.preventDefault();
var selected = new Array();
$('input.chk_address:checked').each(function() {
alert('inside');
selected.push(this.value);
});
console.log(selected);
});
As @Sergio said, you should not use the same id
for several elements. 正如@Sergio所说,你不应该为几个元素使用相同的id
。 Also, your selector $('#chk_address input:checked')
is wrong. 此外,您的选择器$('#chk_address input:checked')
是错误的。 jQuery will consider chk_address
as a parent and look for children elements checked input
. jQuery会将chk_address
视为父级并查找子元素检查input
。
$("#a_send-sms").on("tap",function(e){
e.preventDefault();
var selected = new Array();
$('ul input:checked').each(function() { // or listview id
alert('inside');
selected.push($(this).val()); // instead of .attr("value")
});
console.log(selected);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.