问题
I am trying to get the input value in an each loop of a checkbox, i cant figure out how to get this to work, the value keeps outputting as the first checkbox value.
$('.custemb, input[name=cb], input[class=multadd]').live("click", function() {
$('input[class=multadd]:checked').each(function(index) {
val = index + 2;
valu = $('input[class=multadd]:checked').val();
multiz = multiz + '&aid' + val + '=' + valu;
});
});
the problem is the output of the variable valu
is the first checkbox of the overall each loop, not the current checkbox of the loop, i need the current value.
Any ideas?
回答1:
You can use this
to access the current element in the loop:
valu = $(this).val();
The current element is also sent as a parameter to the callback function, so you can pick it up:
.each(function(index, elem) {
Then use the parameter:
valu = $(elem).val();
回答2:
$('.custemb, input[name=cb], input[class=multadd]').live("click", function() {
$('input[class=multadd]:checked').each(function(index) {
var $this = $(this);
val = index + 2;
valu = $this.val();
multiz = multiz + '&aid' + val + '=' + valu;
});
});
回答3:
Use this
to find the control that was clicked
$('input[class=multadd]:checked').each(function(index) {
val = index + 2;
valu = $(this).val();
multiz = multiz + '&aid' + val + '=' + valu;
});
回答4:
var texts= $(".class_name").map(function() {
return $(this).val();
}).get();
来源:https://stackoverflow.com/questions/10668623/jquery-get-the-input-value-in-an-each-loop