0

我有标准选择,它变成了一个组合框使用jQuery。为什么jQuery不把我的选择seking到选定的值

<select name="Searchby" id="Searchby"> 
    <option value="all" >All Departments</option> 
    <option value="music" >Music</option> 
    <option value="dvd" >DVD</option> 
    <option value="bluray" >Bluray</option> 
    <option value="Artist" >Artist</option> 
</select> 

然后我有一个输入,它有jQuery的自动完成它的类别。当一个人输入到输入中时,它会返回选项以在不同的类别中进行选择。如果他们点击一个选项,我希望它改变上面选择的内容。这是我试过的代码。

$("#Search").catcomplete({ 
    delay: 1000, 
    source: "Drop_Down_Search.php", 
    select: function(event, ui) { 
     if (ui.item.category = 'Artist') { 
      $('#Searchby').val('Artist'); 
      console.log(ui.item.category); 
     } 
    } 
}); 

的控制台登录,它是工作,它正确地发布到搜索结果页面,但你再移动到页面不会改变。它只停留在“所有部门”。我需要改变它,这样一个正在搜索的人可以看到他们只会搜索“艺术家”或“蓝光光芒”,然后才能移动到搜索结果页面。

编辑:

好了,所以事实证明没有jQuery的组合框上的选择将其确实改变了价值,但是当你使用jQuery的组合框不会改变。

一旦选择已被更改为组合框,应该如何更改值?

下面是该组合框代码:

(function($) { 
    $.widget("ui.combobox", { 
     _create: function() { 
      var input, 
       that = this, 
       wasOpen = false, 
       select = this.element.hide(), 
       selected = select.children(":selected"), 
       value = selected.val() ? selected.text() : "", 
       wrapper = this.wrapper = $("<span>") 
        .addClass("ui-combobox") 
        .insertAfter(select); 

      function removeIfInvalid(element) { 
       var value = $(element).val(), 
        matcher = new RegExp("^" + $.ui.autocomplete.escapeRegex(value) + "$", "i"), 
        valid = false; 
       select.children("option").each(function() { 
        if ($(this).text().match(matcher)) { 
         this.selected = valid = true; 
         return false; 
        } 
       }); 

       if (!valid) { 
        // remove invalid value, as it didn't match anything 
        $(element) 
         .val("") 
         .attr("title", value + " didn't match any item") 
         .tooltip("open"); 
        select.val(""); 
        setTimeout(function() { 
         input.tooltip("close").attr("title", ""); 
        }, 2500); 
        input.data("ui-autocomplete").term = ""; 
       } 
      } 

      input = $("<input>") 
       .appendTo(wrapper) 
       .val(value) 
       .attr("title", "") 
       .addClass("ui-state-default ui-combobox-input") 
       .autocomplete({ 
        delay: 0, 
        minLength: 0, 
        source: function(request, response) { 
         var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i"); 
         response(select.children("option").map(function() { 
          var text = $(this).text(); 
          if (this.value && (!request.term || matcher.test(text))) 
           return { 
            label: text.replace(
             new RegExp(
              "(?![^&;]+;)(?!<[^<>]*)(" + 
              $.ui.autocomplete.escapeRegex(request.term) + 
              ")(?![^<>]*>)(?![^&;]+;)", "gi" 
             ), "<strong>$1</strong>"), 
            value: text, 
            option: this 
           }; 
         })); 
        }, 
        select: function(event, ui) { 
         ui.item.option.selected = true; 
         that._trigger("selected", event, { 
          item: ui.item.option 
         }); 
        }, 
        change: function(event, ui) { 
         if (!ui.item) { 
          removeIfInvalid(this); 
         } 
        } 
       }) 
       .addClass("ui-widget ui-widget-content ui-corner-left"); 

      input.data("ui-autocomplete")._renderItem = function(ul, item) { 
       return $("<li>") 
        .append("<a>" + item.label + "</a>") 
        .appendTo(ul); 
      }; 

      $("<a>") 
       .attr("tabIndex", -1) 
       .attr("title", "Select A Critera to Search In ") 
       .appendTo(wrapper) 
       .button({ 
        icons: { 
         primary: "ui-icon-triangle-1-s" 
        }, 
        text: false 
       }) 
       .removeClass("ui-corner-all") 
       .addClass("ui-corner-right ui-combobox-toggle") 
       .mousedown(function() { 
        wasOpen = input.autocomplete("widget").is(":visible"); 
       }) 
       .click(function() { 
        input.focus(); 

        // close if already visible 
        if (wasOpen) { 
         return; 
        } 

        // pass empty string as value to search for, displaying all results 
        input.autocomplete("search", ""); 
       }); 

     }, 

     _destroy: function() { 
      this.wrapper.remove(); 
      this.element.show(); 
     } 
    }); 

    })(jQuery); 
       $(function() { 
     $("#Searchby").combobox(); 
     $("#toggle").click(function() { 
      $("#Searchby").toggle(); 
     }); }); 
+0

没有任何错误代码看起来不错??? – bipen 2013-02-20 08:00:37

+1

不确定它是否是拼写错误,但是if(ui.item.category ='Artist')'行将始终为真,因为您正在设置该值并且未执行比较。 – 2013-02-20 08:01:53

+0

@RubenInfante是一个错字,但它仍然没有改变它在页面上 – Mark 2013-02-20 08:15:01

回答

1

的错误,我发现..

$("#Search").catcomplete({ 
    delay: 1000, 
    source: "Drop_Down_Search.php", 
    select: function(event, ui) { 
     if (ui.item.category == 'Artist') { 
         -----^^-- missing `=` in if since your are comparing this 
      $('#Searchby').val('Artist'); 
      console.log(ui.item.category); 
     } 
    } 
}); 

//}); extra brakets.. 
+0

错字,但仍然没有改变它在页面上 – Mark 2013-02-20 08:15:32

+0

你有什么错误? – bipen 2013-02-20 08:20:02

+0

没有错误。我认为这个问题是因为我将选择改为组合框 – Mark 2013-02-20 08:21:36

相关问题