简体   繁体   English

jQuery为什么不将我的选择设置为所选值

[英]Why is jQuery not seting my select to the selected value

I have standard select which is turned into a combobox using jQuery. 我有使用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 I have an input which has jQuery's autocomplete on it with categories. 然后我有一个输入,它具有jQuery的自动完成类别。 When a person types into the input, it returns options to choose from in different categories. 当一个人键入输入时,它返回选项以从不同类别中进行选择。 If they click on a option I want it to change what is selected in the select above. 如果他们单击一个选项,我希望它更改在上面的选择中选择的内容。 Here is my code that I 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 is logging that it is working and it posts correctly to the search results page but doesn't change before you move off the page. 控制台正在记录其正在运行,并且可以正确地发布到搜索结果页面,但是在移出页面之前不会更改。 It just stays as 'all departments'. 它只是作为“所有部门”。 I need it to change so a person who is searching can see they will only search for 'Artist' or 'Blu-Rays' before they move to the search results page. 我需要对其进行更改,以便正在搜索的人可以看到他们只会在移动到搜索结果页面之前搜索“艺术家”或“蓝光”。

EDIT: 编辑:

Ok so as it turns out without the jquery combobox set on the select it does change the value, but when you use jquery's combobox it doesn't change. 好的,事实证明,没有在select上设置jquery组合框,它的确会更改值,但是当您使用jquery的组合框时,它不会更改。

What should I be using to change the value once a select has been changed into a combobox? 将选择更改为组合框后,应该使用什么来更改值?

Here is the combobox code: 这是组合框代码:

(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();
        }); });

mistake i found.. 我发现的错误..

$( "#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.. 

I found what I was looking for in this answer 我在这个答案中找到了想要的东西

How to define selected value for jQuery UI combobox? 如何定义jQuery UI组合框的选定值?

After some editing I came to this which worked. 经过一些编辑后,我来到了可行的。

$('.ui-autocomplete-input').val($("#Searchby").val("Artist").val());

The value you select is 'Artist' and Searchby is the select id 您选择的值是“艺术家”,Searchby是选择的ID

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

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