jQuery在.each循环中获取输入值

时间:2022-02-04 14:26:34

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.

问题是变量valu的输出是每个循环的第一个复选框,而不是循环的当前复选框,我需要当前值。

Any ideas?

有任何想法吗?

4 个解决方案

#1


33  

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


3  

$('.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


2  

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


1  

var texts= $(".class_name").map(function() {
    return $(this).val();         
}).get();

#1


33  

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


3  

$('.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


2  

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


1  

var texts= $(".class_name").map(function() {
    return $(this).val();         
}).get();