简体   繁体   English

更改 msdropdown 中的选定值

[英]change selected value in msdropdown

I'm trying to use the msDropdown JavaScript plugin ( https://github.com/marghoobsuleman/ms-Dropdown ) to create html tags that include images.我正在尝试使用 msDropdown JavaScript 插件 ( https://github.com/marghoobsuleman/ms-Dropdown ) 创建包含图像的 html 标签。

It seems to work good except for if I try to change the selected value using JavaScript. Like in this jsFiddle: https://jsfiddle.net/cmu845eh/7/ I'm trying to make it so that if you call the setValue() function, it will change the selected value to a certain index.它似乎工作正常,除非我尝试使用 JavaScript 更改选定的值。就像在这个 jsFiddle 中: https://jsfiddle.net/cmu845eh/7/我正在尝试这样做,以便如果您调用 setValue( ) function,它将选择的值更改为某个索引。

I've tried multiple different solutions online but none work, solutions such as this:我在网上尝试了多种不同的解决方案,但都没有用,解决方案如下:

var oHandler = $("#main").msDropDown().data("dd");
oHandler.set("length", 0);

always result in an error, and more basic jquery approaches dont work either.总是会导致错误,更基本的 jquery 方法也不起作用。

Can someone help me change the value of my select tag to a certain index using javascript?有人可以帮助我使用 javascript 将 select 标签的值更改为某个索引吗?

Using msdropdown使用 msdropdown

Let me give you a full code on how to populate select dropdown from server-side using another select dropdown.让我为您提供有关如何使用另一个 select 下拉菜单从服务器端填充 select 下拉菜单的完整代码。

1st Select Dropdown 1st Select 下拉

<select name="" id="from" class="selectFlags" style="width:100%;">
   <option value="2" title="https://fxtop.com/ico/aon.gif">GBP</option>                                                                                                                    
   <option value="3" title="https://fxtop.com/ico/aon.gif">GBP</option>                                                                                                            
</select>

2nd Select Dropdown第二个 Select 下拉

<select name="" id="selectFlagTo" class="selectFlagTo form-control" style="width:100%;height:52px;">
</select>

JScript脚本

<script language="javascript">
        $(document).ready(function(e) {
        try {
            $(".selectFlags").msDropDown({mainCSS:'dd'});
        } catch(e) {
            alert(e.message);
        }
        $(".dd").css({ 'width' : '100%' });
        });
        $(document).on("click",".dd .ddChild",function(e){
            let $from=$("#from option:selected").val();
            let $to=$("#selectFlagTo");
            $to=$to.empty();
                            
            $(".dd").css({ 'width' : '100%' });
            $(".dd2").css({ 'width' : '100%' });
            $.ajax({
                url: "http://odin.test/load_countries",
                type: "POST",
                data: {from:$from},
                success: function(response) {
                    $.each(response, function(key, entry) {
                        $to.append("<option title='https://fxtop.com/ico/aon.gif' value='"+entry.id+"'>"+entry.name+"</option>" );
                    });
                    var oDropdown = $(".selectFlagTo").msDropDown({mainCSS:'dd2'});
                    $(".dd3").css({ 'width' : '100%' });
                }
            });
        });
        </script>

Enjoy coding!!!享受编码!!!

Solved it here by fixing how I was initializing the msdropdown element after the html was created.通过修复我在创建 html 后初始化 msdropdown 元素的方式来解决此问题。 https://jsfiddle.net/uvbjhy31/ https://jsfiddle.net/uvbjhy31/

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

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