javascript 当 Eric Hynds 的带有 JQuery Multiselect UI 的选择框动态更新时自动选择的第一个选项

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

First option automatically selected when select box with JQuery Multiselect UI by Eric Hynds is updated dynamically

javascriptjqueryjquery-uiserver-sent-events

提问by Kris

Im using the JQuery UI multiselect plugin by http://www.erichynds.com/jquery/jquery-ui-multiselect-widget/to dynamically append elements to a select box

我使用http://www.erichynds.com/jquery/jquery-ui-multiselect-widget/的 JQuery UI 多选插件将元素动态附加到选择框

//Make filter cars multiselect
$("#cars_filter").multiselect({noneSelectedText:'Select cars'});

function populateCarfilter(){
    var opts="<option value=''>Select cars</option>";
    $.each(markers, function(idx, mar){
      if(mar.getVisible() && mar.get("car"))
     opts+="<option value='" + mar.get("id") + "'>" + mar.get("driver") + " - " + mar.get("car") + "</option>";
    });

    if($("#cars_filter").html()!=opts){
      var id = $("#cars_filter").val()
      $("#cars_filter").html(opts);
      $("#cars_filter").val(id);
      $("#cars_filter").multiselect('refresh');
    }    
}

populateCarfilter(); //This gets called every 2 secs automatically by SSE (server sent events)

Now, im facing a strange problem. The first option in the select box gets selected automatically everytime the select box is updated. Any way to fix this problem ?

现在,我面临一个奇怪的问题。每次更新选择框时,都会自动选择选择框中的第一个选项。有什么办法可以解决这个问题?

Thank You

谢谢

回答by Miki Shah

Browsers will automatically select the first option unless you add the multiple attribute to the element.

浏览器将自动选择第一个选项,除非您向元素添加 multiple 属性。

See in you jQuery MultiSelect UI Widget javascript source file, They have implemented following

在你的jQuery MultiSelect UI Widget javascript源文件中看到,他们已经实现了以下

// browsers automatically select the first option
// by default with single selects
if( isSelected && !o.multiple ){
    labelClasses.push( 'ui-state-active' );
   }

回答by Jean Paul A.K.A el_vete

and you can always ensure that it works by looping through the elements and setting the value to not selected as well like:

并且您始终可以通过循环遍历元素并将值设置为未选中来确保它正常工作,例如:

  click: function(event, ui){

 if(!ui.checked) 
 {   $.each( $('#select2 option'),function(i2, element2)
       {
                if( $(element2).val() === ui.value )
                {
                   if( $(element2).is(':selected') ) { 
             $(element2).attr('selected',false); 
                }
                     $(element2).remove().appendTo('#select1');
                    }        
        });
 }

}

}

This is just in the event that you are updating dynamically and want to ensure that is so... in my case I was updating / passing over elements from one dropdown to another and both had the multiple='multiple' attribute so I needed to ensure that when clicking in one was being removed, then appending it to the underlying select but still the first option was always selected.. hope it helps someone down the road.. it's a nice plugin

这只是在您动态更新并希望确保如此的情况下...在我的情况下,我正在更新/将元素从一个下拉列表传递到另一个下拉列表,并且都具有 multiple='multiple' 属性,所以我需要确保当点击一个被删除时,然后将它附加到底层选择但仍然选择第一个选项。