简体   繁体   English

下拉菜单(选择)未突出显示已选中=“已选中”选项

[英]Dropdown menu (select) not highlighting selected=“selected” option

I'm building a search form but have something strange going on with the selected="selected" option not being highlighted. 我正在构建搜索表单,但是在执行select =“ selected”选项时未显示出一些奇怪的地方。

The form's pulling data from a database and duplicates are then being removed using javascript (seen below). 然后使用javascript(如下所示)删除表单从数据库中提取的数据和重复项。 The website is built using ExpressionEngine and the form is using Solspace Super Search. 该网站使用ExpressionEngine构建,表单使用Solspace Super Search。

The strange thing is that for option's that only display one result such as 'Andover', the dropdown selects 'Andover' but if more than one result is found (such as Bournemouth) then the dropdown reverts to the first option (All locations) rather than Bournemouth, even though the Bournemouth option features selected="selected". 奇怪的是,对于仅显示一个结果(例如“ Andover”)的选项,下拉列表选择“ Andover”,但是如果找到多个结果(例如伯恩茅斯),则下拉列表将还原为第一个选项(所有位置)即使伯恩茅斯选项的功能为selected =“ selected”,也比伯恩茅斯高。

Here's a sample of the code that's being output, any ideas what's going on here? 这是正在输出的代码的示例,这里有什么想法吗?

<select name="club_feed_town" id="locationList">
    <option value="">All Locations</option>
    <option value="Andover">Andover</option>
    <option value="Bishops waltham">Bishops Waltham</option>
    <option value="Blandford forum">Blandford Forum</option>
    <option value="Boscombe">Boscombe</option>
    <option selected="selected" value="Bournemouth">Bournemouth</option>
</select>

Here's the script that's removing duplicates if that helps: 如果有帮助,以下是删除重复项的脚本:

$(document).ready(function() {
  var optionValues = [];
  var lastRemoved = null;
  $('#locationList option').each(function(){
     if($.inArray(this.value, optionValues) >-1){
        $(this).remove();
        // remember the very last removed one
        lastRemoved = $(this);
     }else{
        optionValues.push(this.value);
     }
  });
  // after removing duplicates, add the very last removed one back to the list
  $('#locationList').append(lastRemoved);
});

As always, any thoughts or suggestions are welcome. 一如既往,欢迎任何想法或建议。

Thanks in advance, 提前致谢,

Tom 汤姆

I just made some add in your js code to make it work. 我只是在您的js代码中添加了一些内容以使其正常工作。 Just give it a look. 看看吧。

$(document).ready(function() {
  var optionValues = [];
  var lastRemoved = null;
  $('#locationList option').each(function(i){
     if($.inArray(this.value, optionValues) >-1){
       //////////////////////add in code///////////////////////////////
        if($(this).prop('selected')){
          $(this).removeAttr("selected");
        }
       $('#locationList').prop('selectedIndex',i-1);
       //////////////////////end of my code//////////////////////////////
       // remember the very last removed one
       $(this).remove();
       lastRemoved = $(this);
     }else{
        optionValues.push(this.value);
     }
  });
  // after removing duplicates, add the very last removed one back to the list
  $('#locationList').append(lastRemoved);
});

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

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