gpt4 book ai didi

javascript - 识别数组中每个对象的属性?

转载 作者:行者123 更新时间:2023-11-30 20:06:31 27 4
gpt4 key购买 nike

我试图在选中框数组的每个对象中识别一个属性,但是这段代码只返回数组中第一个对象的属性的所需元素,用于我选中的框数。这是我的代码:

function() {
var checkedBoxes = $("[name='select-services']:checked");
checkedBoxes.each(function(){
console.log(checkedBoxes.attr("value"))
}

比如三个选中框的值分别是“值1”、“值2”、“值3”,我会收到3次输出“值1”。

最佳答案

点赞attr()的描述说:

Get the value of an attribute for the first element in the set of matched elements or set one or more attributes for every matched element.

您现在正在做的是获取 checkedBoxes 中第一项的 attr。但是您想要的是获取当前正在迭代的项目的值。您可以在函数中使用 $(this) 来做到这一点,如下例所示。

checkedBoxes.each(function(){
console.log($(this).attr("value"))
}

关于javascript - 识别数组中每个对象的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52887462/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com