gpt4 book ai didi

javascript - 仅获取所有选项中间值,使用 jquery 选择(所需)类

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

我有一个下拉列表,其中包含一些从数据库表中检索的值,我想要的是当单击按钮时它应该只获得选项标签的中间值,但只有那些类名为“get_this”的选项标签并离开那些选项,如果他们没有这个类

预期输出:值 1值 3值 4

<!DOCTYPE html>
<html>
<body>

<select id="selectBox">
<option class="get_this">text1 value 1 text1 </option>
<option >text2 value 2 text2</option>
<option class="get_this">text3 value 3 text3</option>
<option class="get_this">text4 value 4 text4</option>
</select>

<input type="submit" value="Get all options" class="get_options"/>

<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script>
var array_values = [];
$(".get_options").click( function (){

var values = $.map($('#selectBox option'), function(ele) {

array_values.push(ele.value);
});

console.log(array_values);
});

</script>
</body>
</html>

最佳答案

<option> 的值时标签需要与其文本内容不同,您可能需要指定 value属性。

<!DOCTYPE html>
<html>
<body>

<select id="selectBox">
<option class="get_this" value="value 1">text1 value 1 text1 </option>
<option value="value 2">text2 value 2 text2</option>
<option class="get_this" value="value 3">text3 value 3 text3</option>
<option class="get_this" value="value 4">text4 value 4 text4</option>
</select>

<input type="submit" value="Get all options" class="get_options"/>

<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script>
var array_values = [];
$(".get_options").click( function (){

var values = $.map($('#selectBox option.get_this'), function(ele) {

array_values.push(ele.value);
});

console.log(array_values);
});

</script>
</body>
</html>

关于javascript - 仅获取所有选项中间值,使用 jquery 选择(所需)类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54618648/

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