gpt4 book ai didi

javascript - 获取 jquery 和 ecmascript6 中的最高属性编号

转载 作者:行者123 更新时间:2023-11-28 17:32:15 26 4
gpt4 key购买 nike

尝试获取具有相同数据属性的所有选择的最大值。但收到错误

{ "message": "Uncaught TypeError: undefined is not a function",
"filename": "https://stacksnippets.net/js", "lineno": 37, "colno": 34 }

请运行下面的代码片段,我希望它能够输出 3,因为这是问题 ID 为 1 的最高响应组编号

$(document).ready(function() {
let question = {
Id: 1
};

let responseGroupNumber = Math.max(...$(`select[data-questionid = ${question.Id}]`)
.map(x => $(x)
.attr('data-responsegroupnumber')));

console.log(responseGroupNumber);

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<select data-questionid="1" data-responsegroupnumber="1">
<option>one</option>
<option>two</option>
<option>three</option>
</select>


<select data-questionid="1" data-responsegroupnumber="2">
<option>one</option>
<option>two</option>
<option>three</option>
</select>

<select data-questionid="1" data-responsegroupnumber="3">
<option>one</option>
<option>two</option>
<option>three</option>
</select>

最佳答案

您需要使用 jQuery .get()映射前的函数:

如果不这样做,您将使用 jQuery .map()方法而不是 Array#map

$(document).ready(function() {
let question = {
Id: 1
};

let responseGroupNumber = Math.max(...$(`select[data-questionid = ${question.Id}]`).get().map(x => $(x).attr('data-responsegroupnumber')));
console.log(responseGroupNumber);

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<select data-questionid="1" data-responsegroupnumber="1">
<option>one</option>
<option>two</option>
<option>three</option>
</select>


<select data-questionid="1" data-responsegroupnumber="2">
<option>one</option>
<option>two</option>
<option>three</option>
</select>

<select data-questionid="1" data-responsegroupnumber="3">
<option>one</option>
<option>two</option>
<option>three</option>
</select>

关于javascript - 获取 jquery 和 ecmascript6 中的最高属性编号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50044810/

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