Javascript 语义 UI:如何定义搜索下拉列表的选定项(“<select>”)?

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

Semantic UI: How to define selected item of search dropdown ("<select>")?

javascriptsemantic-ui

提问by Anton Dachauer

how can I set the selected item of a search dropdown (select) form field? I tried it with the following jQuery/JavaScript code:

如何设置搜索下拉(选择)表单字段的选定项目?我使用以下 jQuery/JavaScript 代码进行了尝试:

if (equal == 0) {
    $('#sj_company').html($('#sj_company').html() + '<option value="' + key + '" selected="selected">' + data[key] + '</option>');
    $('.searchdropdown').dropdown('set selected', data[key]);
} else {
    $('#sj_company').html($('#sj_company').html() + '<option value="' + key + '">' + data[key] + '</option>');
}

Edit: The base is the semantic ui frontend framework ;-)

编辑:基础是语义 ui 前端框架 ;-)

采纳答案by Anton Dachauer

My mistake was: I have initialized the Semantic UI Dropdown beforeI've loaded the dropdown items via AJAX. The solution: Initialize the Dropdown AFTERthe AJAX loading process.

我的错误是:通过 AJAX 加载下拉项之前,我已经初始化了语义 UI 下拉列表。解决方案:在 AJAX 加载过程之后初始化下拉列表。

回答by Cymen

For the Semantic UI framework, you should consult the guide here: dropdown behavior. That tells us to make the following call:

对于语义 UI 框架,您应该参考此处的指南:下拉行为。这告诉我们进行以下调用:

$('#dropdown').dropdown('set selected', value);

$('#dropdown').dropdown('set selected', value);

Or pass an object (perhaps useful for multiple select or to the developer's preference):

或者传递一个对象(可能对多选或开发人员的偏好有用):

$('#dropdown').dropdown({'set selected': value});

$('#dropdown').dropdown({'set selected': value});

Non-framework answer (before question was edited for specific framework)

非框架答案(在针对特定框架编辑问题之前)

If the select list has an id of say dropdown, you can do:

如果选择列表的 id 为 say dropdown,您可以执行以下操作:

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

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

To set the value, say the option has a value of a, you can do:

要设置该值,假设该选项的值为a,您可以执行以下操作:

$('#dropdown').val('a');

$('#dropdown').val('a');

I suggest reading the jQuery page on .val().

我建议阅读.val()上的 jQuery 页面。

JSFiddle example: http://jsfiddle.net/vo9kpsry/1

JSFiddle 示例:http: //jsfiddle.net/vo9kpsry/1

回答by nasir taha

You can simply set the default value by set hidden value input which is exist with semanic dropdown:

您可以通过设置隐藏值输入来简单地设置默认值,该值输入存在于语义下拉列表中:

<div class="dropdown">
    <input type="hidden" name="countries" value="tf">
    <i class="dropdown icon"></i>
    <div class="default text">
    </div>
    <div class="menu">
        <div class="item" data-value="tf"><i class="tf flag"></i>French Territories</div>
        <div class="item" data-value="ga"><i class="ga flag"></i>Gabon</div>
        <div class="item" data-value="gm"><i class="gm flag"></i>Gambia</div>
    </div>
</div>

Maybe too late but someone may need it

也许为时已晚,但有人可能需要它