选择的 Jquery 插件 - 获取选定的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10521315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:27:44  来源:igfitidea点击:

Chosen Jquery Plugin - getting selected values

jqueryjquery-chosen

提问by Aakash Shah

How can i get the selected values from the chosen Multi-select Box?

如何从选定的多选框中获取选定的值?

回答by Ben Truby

$("#select-id").chosen().val()

回答by Sergii

Like from any regular input/select/etc...:

就像从任何常规输入/选择/等...:

$("form.my-form .chosen-select").val()

回答by Arvind

This worked for me

这对我有用

$(".chzn-select").chosen({

     disable_search_threshold: 10

}).change(function(event){

     if(event.target == this){
        alert($(this).val());
     }

});

回答by user3423551

$("#select-id").chosen().val()

this is the right answer, I tried, and the value passed is the values separated by ","

这是正确的答案,我试过了,传递的值是用“,”分隔的值

回答by Panagiotis Koursaris

If anyone wants to get only the selected value on click to an option, he can do the follow:

如果有人只想在单击选项时获得选定的值,他可以执行以下操作:

$('.chosen-select').on('change', function(evt, params) {
    var selectedValue = params.selected;
    console.log(selectedValue);
});

回答by BobRodes

As of 2016, you can do this more simply than in any of the answers already given:

截至 2016 年,您可以比已经给出的任何答案更简单地做到这一点:

$('#myChosenBox').val();

where "myChosenBox" is the id of the original select input. Or, in the change event:

其中“myChosenBox”是原始选择输入的 ID。或者,在更改事件中:

$('#myChosenBox').on('change', function(e, params) {
    alert(e.target.value); // OR
    alert(this.value); // OR
    alert(params.selected); // also in Panagiotis Kousaris' answer
}

In the Chosen doc, in the section near the bottom of the page on triggering events, it says "Chosen triggers a number of standard and custom events on the original select field." One of those standard events is the change event, so you can use it in the same way as you would with a standard select input. You don't have to mess around with using Chosen's applied classes as selectors if you don't want to. (For the changeevent, that is. Other events are often a different matter.)

Chosen doc页面底部附近的触发事件部分,它说“Chosen 在原始选择字段上触发了许多标准和自定义事件。” 这些标准事件之一是更改事件,因此您可以像使用标准选择输入一样使用它。如果您不想,您不必纠结于使用 Chosen 的应用类作为选择器。(对于change事件,即。其他事件通常是另一回事。)

回答by Hoàng V? Tgtt

if you want to get text of a selected option (chosen get display selected value)

如果要获取所选选项的文本(选择获取显示所选值)

 $("#select-id").chosen().find("option:selected" ).text();

回答by Osogtulak

This solution worked for me

这个解决方案对我有用

var ar = [];
$('#id option:selected').each(function(index,valor){
    ar.push(valor.value);
});
console.log(ar);

be sure to have your <option>tags with they correspondant valueattribute. Hope it helps.

确保您的<option>标签具有相应的value属性。希望能帮助到你。

回答by John James Jacoby

I believe the problem occurs when targeting by ID, because Chosen will copy the ID from the original selectonto it's newly created div, leaving you with 2 elements of the same (now not-unique) ID on the current page.

我相信按 ID 定位时会出现问题,因为 Chosen 会将原始 ID 复制select到新创建的divID 上,在当前页面上留下 2 个相同(现在不是唯一的)ID 的元素。

When targeting Chosen by ID, use a selector specific to the select:

定位按 ID 选择时,请使用特定于以下内容的选择器select

$( 'select#yourID' ).on( 'change', function() {
    console.log( $( this ).val() );
} );

...instead of...

...代替...

$( '#yourID' ).on( 'change', function() {
    console.log( $( this ).val() );
} );

This works because Chosen mirrors its selected items back to the original (now hidden) selectelement, even in multi-mode.

这是有效的,因为select即使在多模式下,Chosen也会将其选定的项目镜像回原始(现在隐藏)的元素。

(It also continues to work with or without Chosen, say... if you decide to go a different direction in your application.)

(无论是否使用 Chosen,它都会继续工作,例如……如果您决定在应用程序中采用不同的方向。)