简体   繁体   English

创建一个按钮以删除select2上的所选值

[英]create a button to delete choosen value on select2

I want to ask about select2. 我想问一下select2。 I have 2 select2, when client choose the list. 当客户选择列表时,我有2个select2。 after the client choose the list, the button show up with name of the value choose and the value go to button instead on input area select2 so the the select2 will stay with the placeholder. 客户选择列表后,将显示带有值select名称的按钮,然后在输入区域select2上将值转到按钮,因此select2将保留在占位符中。

<button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1">
    <!-- the name of button is the value of what we choose in first select2  /example Alabama-->
    <span class="glyphicon glyphicon-remove"></span>
 </button>

the button have function to allowClear: true like. 该按钮具有allowClear:true之类的功能。 how to be like that i put it on jsfiddle https://jsfiddle.net/mujkz5v3/1/ 怎么样我把它放在jsfiddle https://jsfiddle.net/mujkz5v3/1/

thank you so much, please help 非常感谢你,请帮忙

Use $("#dropdownMenu1").html( deggre ); 使用$(“#dropdownMenu1”)。html(deggre);

or $("#dropdownMenu1").text( deggre ); 或$(“#dropdownMenu1”)。text(deggre);或

instead of .val 代替.val

See the following changes: 请参阅以下更改:

$('.deggre').on('change', function() {
    var deggre = $( this ).val();
    var span = $("#dropdownMenu1").find('span').clone(); // Copy 'X' span.
    $("#dropdownMenu1").text(deggre); // Insert text first.
    $("#dropdownMenu1").append(span); // Then, append the 'X' span.
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM