Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

javascript - jQuery get the input value in an .each loop

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...