jQuery在.each循环中获取输入值
16lz
2021-01-22
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();
更多相关文章
- 在传递给google.setOnLoadCallback()的函数中使用参数;
- Js的Url中传递中文参数乱码,如何获取Url中参数问题
- Javascript setTimeout 带参数延迟执行 闭包实现
- Angularjs在ng-repeat中找到最后一个可见元素
- 浏览器如何处理HTML元素上的无效/未指定的属性?
- 如何使用jQuery在GET请求中传递参数
- 为什么推荐把<script>元素写在body里面
- 在bootstrap中为同一元素使用两个数据切换
- 如何获取knockoutjs可观察数组的下一个元素?