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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10668623/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 02:26:14  来源:igfitidea点击:

jQuery get the input value in an .each loop

javascriptjqueryeach

提问by JimmyBanks

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 valuis the first checkbox of the overall each loop, not the current checkbox of the loop, i need the current value.

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

Any ideas?

有任何想法吗?

回答by Guffa

You can use thisto access the current element in the loop:

您可以使用this来访问循环中的当前元素:

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();

回答by Kyle Macey

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

回答by web_bod

Use thisto find the control that was clicked

使用this发现被点击的控制

$('input[class=multadd]:checked').each(function(index) {
        val = index + 2;
        valu = $(this).val();
        multiz = multiz + '&aid' + val + '=' + valu;
    });

回答by sadam

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