javascript - Why is jQuery not seting my select to the selected value -
i have standard select turned combobox using jquery.
<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>
then have input has jquery's autocomplete on categories. when person types input, returns options take in different categories. if click on alternative want alter selected in select above. here code tried.
$("#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); } } });
the console logging working , posts correctly search results page doesn't alter before move off page. stays 'all departments'. need alter person searching can see search 'artist' or 'blu-rays' before move search results page.
edit:
ok turns out without jquery combobox set on select alter value, when utilize jquery's combobox doesn't change.
what should using alter value 1 time select has been changed combobox?
here combobox code:
(function( $ ) { $.widget( "ui.combobox", { _create: function() { var input, = 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 ( $( ).text().match( matcher ) ) { this.selected = valid = true; homecoming false; } }); if ( !valid ) { // remove invalid value, didn't match $( element ) .val( "" ) .attr( "title", value + " didn't match 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 = $( ).text(); if ( this.value && ( !request.term || matcher.test(text) ) ) homecoming { label: text.replace( new regexp( "(?![^&;]+;)(?!<[^<>]*)(" + $.ui.autocomplete.escaperegex(request.term) + ")(?![^<>]*>)(?![^&;]+;)", "gi" ), "<strong>$1</strong>" ), value: text, option: }; }) ); }, 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( ); } } }) .addclass( "ui-widget ui-widget-content ui-corner-left" ); input.data( "ui-autocomplete" )._renderitem = function( ul, item ) { homecoming $( "<li>" ) .append( "<a>" + item.label + "</a>" ) .appendto( ul ); }; $( "<a>" ) .attr( "tabindex", -1 ) .attr( "title", "select critera 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 visible if ( wasopen ) { return; } // pass empty string value search for, displaying results input.autocomplete( "search", "" ); }); }, _destroy: function() { this.wrapper.remove(); this.element.show(); } }); })( jquery ); $(function() { $( "#searchby" ).combobox(); $( "#toggle" ).click(function() { $( "#searchby" ).toggle(); }); });
mistake found..
$( "#search" ).catcomplete({ delay: 1000, source: "drop_down_search.php", select: function( event, ui ) { if (ui.item.category == 'artist') { -----^^-- missing `=` in if since comparing $('#searchby').val('artist'); console.log(ui.item.category); } } }); //}); brakets..
javascript jquery jquery-ui jquery-autocomplete html-select
No comments:
Post a Comment