jQuery select2怎样获取select标签的值?

112

大家好,这是我的代码:

<select id='first'>
  <option value='1'> First  </option>
  <option value='2'> Second </option>
  <option value='3'> Three  </option>
</select>

这是我的select2代码:

$("#first").select2();

下面是获取选定值的代码。

$("#first").select2('val'); // It returns first,second,three.

这将返回一个文本,例如first,second,three,我想要得到1,2,3
这意味着我需要选择框的值,而不是文本。


你能提供一个JSFiddle吗?这将非常有用。 - Ionică Bizău
1
问题不清楚。您到底想要什么? - codingrose
12个回答

183
$("#first").val(); // this will give you value of selected element. i.e. 1,2,3.

5
$("#first").val(); // 它返回1、2、3 或者 $("#first").select2('val'); // 它返回first、second、three - Renish Khunt
$("#first").val(); 将返回1、2、3(无论选择哪个),您可以在函数select2()中发布代码以获取更多详细信息。 - somesh
使用按ID名称调用的选择器,而不是按类名调用的选择器:$(“#first”).val() - Rui Martins
@RuiMartins 错了。ID 用 "#" 来调用,而类别则用 "." 来调用。因此,要通过 ID 调用,您需要使用 $("#first").val(),而对于类别,则需要使用 $(".first").val()。 - Source Matters

68

要获取选择元素,您可以使用$('#first').val();

要获取所选值的文本 - $('#first :selected').text();

请发布您的select2()函数代码


1
$("#first").val(); // 它返回1,2,3 或者 $("#first").select2('val'); // 它返回first,second,three - Renish Khunt
我不确定,我是否理解了你的问题。 - Krish R
1
当我使用select2插件时,我使用$("#first").val()获取值。它没有返回任何东西,而是返回空白。 - Renish Khunt
你能提供一个带有你的插件的 Fiddle 示例吗? - Krish R
1
我不得不使用 $('#first :selected').text();,因为另一个选项会返回所有可能的选项。 - Jeff Bluemel
$input.find(':selected').text() 返回空字符串。 - Vasilii Suricov

43

$("#first").select2('data')会返回一张包含所有数据的映射表


1
如果您在 select2 对象上有额外属性,这将非常有用。 - jamesSampica
https://select2.org/programmatic-control/add-select-clear-items - Kamlesh

16

如果你正在使用ajax,你可能想在选择后立即获取


你好,我想问一下 currentTarget 是从哪里来的?你提供的 ID/名称示例都与问题无关,所以我无法联系到任何地方。 - mastersuse
非常感谢您,我的主人 :p 祝编码愉快。 - Vipertecpro

9

这个解决方案允许您忘记选择元素。当您在选择元素上没有id时非常有用。

$("#first").select2()
.on("select2:select", function (e) {
    var selected_element = $(e.currentTarget);
    var select_val = selected_element.val();
});

8
简单回答是:
$('#first').select2().val()

你也可以这样写:

 $('#first').val()

4
尝试这个:

试试看:

$('#input_id :selected').val(); //for getting value from selected option
$('#input_id :selected').text(); //for getting text from selected option

@RenishKhunt 这里的答案不仅适用于你(提问者),也适用于看到这个页面的其他人。而且,提交更多不同的解决方案,每个人都会有更好的选择,再次强调:不仅仅是你 :) - infografnet
1
是的,我很抱歉。感谢@infografnet和非常感谢Fahmi Imanudin发布答案 :) - Renish Khunt

4

它只返回所选值中的第一个数值 - undefined

3
$(".element").select2(/*Your code*/)
.on('change', function (e) {
     var getID = $(this).select2('data');
     alert(getID[0]['id']); // That's the selected ID :)
});

虽然您的回答可能解决了问题,但包括解释这个问题如何解决的方式和原因,将真正有助于提高您的帖子质量,并可能导致更多的赞。请记住,您正在回答未来读者的问题,而不仅仅是现在提问的人。您可以编辑您的答案以添加解释,并指出适用的限制和假设。- 来自审核 - Adam Marshall

2

请查看这个示例
基本上,您想获取option标签的value,但您总是尝试使用$("#first").val()获取select节点的值。

因此,我们需要选择option标签,并利用jQuery选择器:

$("#first option").each(function() {
    console.log($(this).val());
});

$("#first option") 选择所有 id 为 first 的元素下的所有 option 子元素。


$("#first").val(); // 它返回1,2,3 或者 $("#first").select2('val'); // 它返回first,second,three - Renish Khunt
似乎我误解了你的问题,请明确你想要实现什么。 - KeyNone
当我使用select2插件时,我使用$("#first").val()获取值。它没有返回任何东西,而是返回空白。 - Renish Khunt
查看这个 Fiddle,对我来说运作得非常完美。(虽然我已经在外部资源中添加了 select2.js,但我无法确定为什么框看起来很丑陋)。 - KeyNone
关键是你说我们想要获取选项标签的值! - Dariux

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接