gpt4 book ai didi

javascript - jquery 将元素存储到 vars

转载 作者:行者123 更新时间:2023-11-30 20:45:14 26 4
gpt4 key购买 nike

我有一个 <select> 的列表标签,共22个。我用 .find('className');将它们全部存储在数组中,然后遍历它们以获取每个值。

问题是我如何仅使用 jquery 准确地做到这一点?因为如果我尝试使用 .val()在已经存储在变量中的项目上,我将收到错误 .val() is not a function .那么一旦元素被存储到 var 中会发生什么?现在它看起来像这样:

var foo = $('.products_container').find('.fn_variant option:selected'); 

所有选择的选项。每个看起来像 -

<option selected="selected" value="13855" data-price="5 600" data-city_id="5" data-stock="65" data-multiplicity="1">
</option>

循环代码:

for(f in foo) {
if (typeof foo[f] == 'object') {
console.log(foo[f].dataset.city_id);
// this is the way i'm accessing values right now, which is inconvenient and unclear.
}
}

最佳答案

您可以使用:

var $foo = $('.products_container').find('.fn_variant option:selected'); 

和:

$foo.each(function(){
let $cityID = $(this).attr('data-city_id');
});

关于javascript - jquery 将元素存储到 vars,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48786736/

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