views:

399

answers:

1

I am trying to change my comboboxes to use autocomplete so i leverage the code listed here (which worked perfectly for my dropdowns)

The issue is that i also on the same page have a listbox with the following code:

<%= Html.ListBox("Cars", Model.BodyParts.Select(
                    x => new SelectListItem {
                        Text = x.Name,
                        Value = x.Id,
                        Selected = Model.CarsSelected.Any(y => y.Id == x.Id)
                    }
               ))%>

and it appears that the jquery ui code is changing this to a autocomplete dropdown as well (as opposed to keeping it as a multi select list box)

any idea how to prevent this from happening?

i literally am just using the exact code on this page

<script type="text/javascript">
(function($) {
    $.widget("ui.combobox", {
        _create: function() {
            var self = this;
            var select = this.element.hide();
            var input = $("<input>")
                .insertAfter(select)
                .autocomplete({
                    source: function(request, response) {
                        var matcher = new RegExp(request.term, "i");
                        response(select.children("option").map(function() {
                            var text = $(this).text();
                            if (!request.term || matcher.test(text))
                                return {
                                    id: $(this).val(),
                                    label: text.replace(new RegExp("(?![^&;]+;)(?!<[^<>]*)(" + request.term.replace(/([\^\$\(\)\[\]\{\}\*\.\+\?\|\\])/gi, "\\$1") + ")(?![^<>]*>)(?![^&;]+;)", "gi"), "<strong>$1</strong>"),
                                    value: text
                                };
                        }));
                    },
                    delay: 0,
                    select: function(e, ui) {
                        if (!ui.item) {
                            // remove invalid value, as it didn't match anything
                            $(this).val("");
                            return false;
                        }
                        $(this).focus();
                        select.val(ui.item.id);
                        self._trigger("selected", null, {
                            item: select.find("[value='" + ui.item.id + "']")
                        });

                    },
                    minLength: 0
                })
                .addClass("ui-widget ui-widget-content ui-corner-left");
            $("<button>&nbsp;</button>")
            .insertAfter(input)
            .button({
                icons: {
                    primary: "ui-icon-triangle-1-s"
                },
                text: false
            }).removeClass("ui-corner-all")
            .addClass("ui-corner-right ui-button-icon")
            .position({
                my: "left center",
                at: "right center",
                of: input,
                offset: "-1 0"
            }).css("top", "")
            .click(function() {
                // close if already visible
                if (input.autocomplete("widget").is(":visible")) {
                    input.autocomplete("close");
                    return;
                }
                // pass empty string as value to search for, displaying all results
                input.autocomplete("search", "");
                input.focus();
            });
        }
    });

})(jQuery);

$(function() {
    $("select").combobox();
});
</script>
+2  A: 

I'm guessing that your JQuery selector (not provided in your question) is grabbing all dropdowns on the page. You should look at making it more specific, most likely referencing the element by ID. There are lots of good examples in the JQuery docs:

http://api.jquery.com/category/selectors/

Neal Swearer
if there is only one dropdown to select, by all means use an id. however, it seems as though he might want to select multiple dropdowns, making class-names a better solution. but i certainly agree that it is an overzealous selector at fault here.
geowa4
@geowa4 - i have updated the question to show the exact javascript code that i am using directly off the jquery site
ooo
@Neal Swearer - i have updated the question to show the exact javascript code that i am using directly off the jquery site
ooo
The third line from the bottom is your problem. That JQuery selector... $("select")........will grab all HTML Select controls on the page.
Neal Swearer
@Neal Swearer - thanks . . i change that to a class selector and that solved the problem
ooo