[英]Return Multiple CheckBox Values if Checked in jQuery
I have multiple checkbox in my page. 我的页面上有多个复选框。 i want to retrieve its values if checked.
我想检索其值,如果选中。
Here is HTML Code.. 这是HTML代码。
<input name="ctl1189" type="checkbox" id="chkTicket_189310" class=" chkTicket" value="189310">
<input name="ctl1190" type="checkbox" id="chkTicket_189311" class=" chkTicket" value="189311">
And So on.. 等等..
Javascript Code: JavaScript代码:
function updateTicketAction() {
var allUpdatedVendorTickets = $('.chkTicket').filter(function() { return this.value != $input.is(':unchecked'); });
var sFinalUpdateList = '';
allUpdatedVendorTickets.each(function() {
var ids = $(this).attr('id').split('_')[1];
sFinalUpdateList += ((sFinalUpdateList == '') ? '' : '|') + ids + ',' + $(this).val();
});
alert(sFinalUpdateList);
}
function updateTicketAction() {
var sFinalUpdateList = '';
$("input.chkTicket:checked").each(
function() {
var ids = $(this).attr('id').split('_');
var id = ids[1];
sFinalUpdateList += ((sFinalUpdateList == '') ? '' : '|') + id + ',' + $(this).val();
}
);
alert(sFinalUpdateList);
}
http://jquery-howto.blogspot.com/2008/12/how-to-check-if-checkbox-is-checked.html http://jquery-howto.blogspot.com/2008/12/how-to-check-if-checkbox-is-checked.html
try this code 试试这个代码
var collect='';
$('input:checked').each(function(i,e){
collect+=(collect===''? $(e).attr('name')+'='+$(e).val():','+$(e).attr('name')+'='+$(e).val());
alert(collect);
})
I just created a fiddle . 我只是制造了一个小提琴 。 Look in the JavaScript console for the resulting object, which you can then further process.
在JavaScript控制台中查找结果对象,然后可以对其进行进一步处理。
Code: 码:
var checkedCheckboxes = $('.chkTicket:checked'),
results = {};
checkedCheckboxes.each(function () {
var key = $(this).attr('id').split('_')[1];
results[key] = $(this).val();
});
function alertResults(results) {
var text = '', sep = '';
$.each(results, function (key, value) {
text += sep + key + '=' + value;
sep = '|';
});
alert(text);
}
alertResults(results);
console.log(results);
Here is a jsfiddle snippet that returns all checked inputs.. One question though, why do you split your id-attribute when you have your id stored in value-attribute? 这是一个jsfiddle片段 ,它返回所有已检查的输入。尽管一个问题,当将id存储在value-attribute中时,为什么要拆分id-attribute? Anyways, hope this works for you!
无论如何,希望这对您有用!
function updateTicketAction() {
var allUpdatedVendorTickets = $('.chkTicket:checked');
var sFinalUpdateList = '';
allUpdatedVendorTickets.each(function () {
sFinalUpdateList += ((sFinalUpdateList == '') ? '' : '|') + $(this).val();
});
alert(sFinalUpdateList);
}
Or you can use map(): 或者您可以使用map():
var el = $('input[type=checkbox]:checked').map(function(i,e){
var id = $(e).attr('id').replace('chkTicket_', '');
var val = $(e).val();
return {
'id' : id,
'value' : val
}
});
console.log(el[0].id);
Your filter function is wrong. 您的过滤器功能错误。
this.value != $input.is(':unchecked');
You're comparing a .value
property (string) to the return value of the .is
jQuery method (boolean). 您正在将
.value
属性(字符串)与.is
jQuery方法(布尔值)的返回值进行比较。 It's similar to: 它类似于:
'value' != !true //checked
'value' != !false //unchecked
Both will always return true
- unless value
is 0
or an empty string which evaluates to a falsy value and the right side of the expression evaluates to false
, for which the !=
different operator will return false
. 双方将始终返回
true
-除非value
是0
或者将计算得到一个falsy值和表达的右侧空字符串的计算结果为false
,为此, !=
不同的运营商将返回false
。
So, your filter
's callback doesn't filter anything at all, except taking out checked
boxes with value 0
or no value (which is unintentional). 所以,你的
filter
的回调完全不过滤任何东西,除了取出checked
与储值箱0
或没有值(这是无意的)。
You can avoid using a filter
function by using the :checked
selector: 您可以通过使用
:checked
选择器来避免使用filter
功能:
var allUpdatedVendorTickets = $('.chkTicket:checked');
Now you'll have a jQuery object containing only the checked .chkTicket
, much better. 现在,您将拥有一个仅包含选中的
.chkTicket
的jQuery对象,更好。
Next thing, you're making bad use of strings. 接下来,您在滥用字符串。
"189310,189310|189311,189311"
That's what your function is generating. 这就是您的函数所生成的。 Every time you need to manipulate those results, you'll have to split the string at
|
每次需要处理这些结果时,都必须在
|
处拆分字符串。 , creating a new array. ,创建一个新数组。 It's much better to store it as an array already.
最好将其存储为数组。
var sFinalUpdateList = [];
Assuming your keys and values are always the same as in your example, you should store them only once as well. 假设您的键和值始终与示例中的相同,则也应该只存储一次。
allUpdatedVendorTickets.each(function() {
sFinalUpdateList.push(this.value);
});
This will generate a much more clean and maintainable array with only the checked boxes' values: 这将生成一个更加干净和可维护的数组,其中仅包含复选框的值:
sFinalUpdateList =>
[0] -> "189310"
[1] -> "189311"
You can obtain their IDs by appending chkTicket_
to those values. 您可以通过将
chkTicket_
附加到这些值来获取其ID。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.