javascript 下拉清除选项语义-ui
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32251443/
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
Dropdown clear option semantic-ui
提问by user3040942
I've encountered a problem when trying to implement 2 dropdowns in semantic-ui.
我在语义 ui 中尝试实现 2 个下拉菜单时遇到了问题。
What I want to happen is that when I change the selected item in the first dropdown the second dropdown will be automatically be cleared from its pervious selection (this is the first step in implementing an automatic change to the content of the second dropdown).
我想要发生的是,当我更改第一个下拉列表中的选定项目时,第二个下拉列表将自动从其先前的选择中清除(这是对第二个下拉列表的内容实现自动更改的第一步)。
I've created a simpler version containing 2 dropdowns and a clear button in JS bin : here.
我在 JS bin 中创建了一个更简单的版本,其中包含 2 个下拉菜单和一个清除按钮:here。
The code:
代码:
var $one = $('#one'),
$two = $('#two');
$two.dropdown();
$one.dropdown({
onChange :function(val) {
$two.dropdown('clear');
}
});
$('.button').on('click', function() {
$('#two').dropdown('clear');
});
Here I've encountered multiple problems:
在这里我遇到了多个问题:
The first problem I encountered was that the clear button won't clear both dropdowns unless both dropdowns are only initialised and no other settings added in the initialisation (i.e $(".ui.dropdown").dropdown()).
我遇到的第一个问题是清除按钮不会清除两个下拉列表,除非两个下拉列表都只被初始化并且没有在初始化中添加其他设置(即 $(".ui.dropdown").dropdown())。
The second problem is that in the provided code the a dropdown will be cleared (only the second dropdown will be cleared for some reason, and not the first) only if the selector for the clear button will be $(".ui.dropdown) , if I use $("#one")/$("#two") the button won't clear the dropdown.
第二个问题是,在提供的代码中,只有当清除按钮的选择器为 $(".ui.dropdown) ,如果我使用 $("#one")/$("#two") 按钮将不会清除下拉列表。
And the third problem , is that when a change occurs in the first dropdown the second dropdown is not being cleared , which is my ultimate goal.
第三个问题是,当第一个下拉列表发生变化时,第二个下拉列表没有被清除,这是我的最终目标。
Any thoughts or suggestions will be greatly appreciated.
任何想法或建议将不胜感激。
回答by Ritesh Kumar
$(document).ready(function () {
$("#super_type").dropdown({
onChange: function (val) {
$('#subtype_list').dropdown('clear');
});
});
回答by Kshitij
This is the best and easy solution provided by semantic UI.
$('.ui.dropdown').dropdown({
"clearable": true
});
By default the value of clearable
setting is false.
这是语义 UI提供的最好和简单的解决方案。
$('.ui.dropdown').dropdown({
"clearable": true
});
默认情况下,clearable
设置的值为 false。
回答by itssadon
See http://semantic-ui.com/modules/dropdown.html#behavior
请参阅http://semantic-ui.com/modules/dropdown.html#behavior
$('.your.element').dropdown('set selected', "value");
$('.your.element').dropdown('set selected', "value");
This worked for me.
这对我有用。
回答by Israel Zebulon
Using only js/jquery
仅使用 js/jquery
var elemento = $("idElementoSelect).next().next();
elemento.html('text';)
回答by Alsmile
see http://www.semantic-ui.cn/modules/dropdown.html#/settings
见http://www.semantic-ui.cn/modules/dropdown.html#/settings
$('select.dropdown').dropdown({placeholder:''});