jQuery 单击时更改 select2 选项

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13987379/
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 13:09:55  来源:igfitidea点击:

Change select2 option on click

jqueryjquery-select2

提问by CR41G14

I am using the select2 plugin on my select services_ddin my form, I have a link on the page with a class .js-autoselect. On clicking this I want to take the ID of that link tag and change the select2 option depending on the button they clicked.

services_dd在我的表单中使用 select2 插件,我在页面上有一个带有 class 的链接.js-autoselect。单击此按钮时,我想获取该链接标签的 ID,并根据他们单击的按钮更改 select2 选项。

The <option>value attribute matches the attribute of the id on the link. The code below is successfully changing the value but the select2 is not updating, it still shows the first selection.

<option>值属性的链接的id属性相匹配。下面的代码成功更改了值,但 select2 没有更新,它仍然显示第一个选择。

$('.js-autoselect').click(function(e){
    e.preventDefault();
    var option = $(e.target).attr('id').trim().toLowerCase(); // option =link1

    $('#services_dd option:selected').val(option);
    alert($('#services_dd option:selected').val()); // this alerts link1 which is correct

});

Does anyone know how to update the select2

有谁知道如何更新select2

回答by scoota269

$("#services_dd").select2("val", option);

$("#services_dd").select2("val", option);

See the Programmatic Access section of the documentation: http://ivaynberg.github.com/select2/#documentation

请参阅文档的编程访问部分:http: //ivaynberg.github.com/select2/#documentation

回答by Robert Massaioli

Working example

工作示例

Based on @scoota269's answer I wrote the following JS Fiddle that shows this working: https://jsfiddle.net/robertmassaioli/ro2279ts/5/

基于@scoota269 的回答,我写了以下 JS Fiddle 来显示这个工作:https://jsfiddle.net/robertmassaioli/ro2279ts/5/

That should be able to help you see how to make this work.

这应该能够帮助您了解如何进行这项工作。

Code

代码

Just in case JS Fiddle is ever shut down:

以防万一 JS Fiddle 被关闭:

$(function() {
    var select = $("#the-select");
    select.select2();

    $("#update").click(function() {
        // How to remove a value
        select.find("option[value=1]").remove();
        // How to add a value
        select.append("<option value='5'>Five</option>");
        select.select2();
    });

    $("#select").click(function() {
        // How to select a value
        var current = select.select2("val");
        current = current || [];
        current.push(2);
        select.select2("val", current);
    });
});

回答by Aioros

I think you should simply do

我认为你应该简单地做

$('#services_dd').val(option);
alert($('#services_dd').val());

Otherwise, in your code, you would only change the value attribute of the selected option (instead of the value of the select element).

否则,在您的代码中,您只会更改所选选项的 value 属性(而不是 select 元素的值)。