Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用Json自动填充用户表单传递下拉组合框值_Javascript_Jquery_Json_Asp.net Mvc_Combobox - Fatal编程技术网

Javascript 使用Json自动填充用户表单传递下拉组合框值

Javascript 使用Json自动填充用户表单传递下拉组合框值,javascript,jquery,json,asp.net-mvc,combobox,Javascript,Jquery,Json,Asp.net Mvc,Combobox,我有一个下拉/组合框,它是这样制作的 @Html.DropDownList("combobox", Model.Sizes, "--select--") 它使用一个jquery ui小部件 <script> (function ($) { $.widget("custom.combobox", { _create: function () { this.wrapper = $("<span

我有一个下拉/组合框,它是这样制作的

 @Html.DropDownList("combobox", Model.Sizes, "--select--")
它使用一个jquery ui小部件

   <script>
    (function ($) {
        $.widget("custom.combobox", {
            _create: function () {
                this.wrapper = $("<span>")
                  .addClass("custom-combobox")
                  .insertAfter(this.element);

                this.element.hide();
                this._createAutocomplete();
                this._createShowAllButton();
            },

            _createAutocomplete: function () {
                var selected = this.element.children(":selected"),
                  value = selected.val() ? selected.text() : "";

                this.input = $("<input>")
                  .appendTo(this.wrapper)
                  .val(value)
                  .attr("title", "")
                  .addClass("custom-combobox-input ui-widget ui-widget-content ui-state-default ui-corner-left")
                  .autocomplete({
                      delay: 0,
                      minLength: 0,
                      source: $.proxy(this, "_source"),
//ADDED HERE
                      select: function(event, ui) { 
                    sizeID=ui.item.id; 
                }
                  })
                  .tooltip({
                      tooltipClass: "ui-state-highlight"
                  });

                this._on(this.input, {
                    autocompleteselect: function (event, ui) {
                        ui.item.option.selected = true;
                        this._trigger("select", event, {
                            item: ui.item.option
                        });
                    },

                    autocompletechange: "_removeIfInvalid"
                });
            },

            _createShowAllButton: function () {
                var input = this.input,
                  wasOpen = false;

                $("<a>")
                  .attr("tabIndex", -1)
                  .attr("title", "Show All Items")
                  .tooltip()
                  .appendTo(this.wrapper)
                  .button({
                      icons: {
                          primary: "ui-icon-triangle-1-s"
                      },
                      text: false
                  })
                  .removeClass("ui-corner-all")
                  .addClass("custom-combobox-toggle ui-corner-right")
                  .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", "");
                  });
            },

            _source: function (request, response) {
                var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i");
                response(this.element.children("option").map(function () {
                    var text = $(this).text();
                    if (this.value && (!request.term || matcher.test(text)))
                        return {
                            label: text,
                            value: text,
                            option: this
                        };
                }));
            },

            _removeIfInvalid: function (event, ui) {

                // Selected an item, nothing to do
                if (ui.item) {
                    return;
                }

                // Search for a match (case-insensitive)
                var value = this.input.val(),
                  valueLowerCase = value.toLowerCase(),
                  valid = false;
                this.element.children("option").each(function () {
                    if ($(this).text().toLowerCase() === valueLowerCase) {
                        this.selected = valid = true;
                        return false;
                    }
                });

                // Found a match, nothing to do
                if (valid) {
                    return;
                }

                // Remove invalid value
                this.input
                  .val("")
                  .attr("title", value + " didn't match any item")
                  .tooltip("open");
                this.element.val("");
                this._delay(function () {
                    this.input.tooltip("close").attr("title", "");
                }, 2500);
                this.input.autocomplete("instance").term = "";
            },

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

    $(function () {
        $("#combobox").combobox();
        $("#toggle").click(function () {
            $("#combobox").toggle();
        });
    });
</script>
    public JsonResult GetSize(int sizeID)
    {
        var Size = db.AllSizes.Find(sizeID);
        return Json(Size, JsonRequestBehavior.AllowGet);
    }
ddddd这个功能在我正常的下拉列表中运行得非常好,但一旦我将它改为组合框,它似乎就没有得到sizeID


HELP BENJI

您可以添加如下所述的选择功能:

因此,当选择一个选项时,您将该值存储在一个变量中

例如:


因此,如果我在组合框的自动完成代码中插入select函数,它将自动传递大小id?我更新了我的问题以显示我在何处添加了select函数,这目前仍然没有任何作用。您还需要在函数范围之外定义sizeID变量。还更新了您是否能够使用浏览器开发人员工具并在选择函数内设置断点以确保调用该函数?我已在范围外添加了变量并在“$[name='combobox']内使用了.alert。changefunction{我会更新上面的两个,但是这个警报没有被调用,这意味着它没有在下拉列表的变化中拾取下拉列表?是的,我放了一个警报,它没有工作,因为它没有在变化中拾取下拉列表,我用一个正常的下拉列表测试了它,没有组合框功能,它工作正常,警报被调用
    public JsonResult GetSize(int sizeID)
    {
        var Size = db.AllSizes.Find(sizeID);
        return Json(Size, JsonRequestBehavior.AllowGet);
    }
var sizeID;
.autocomplete({
                          delay: 0,
                          minLength: 0,
                          source: $.proxy(this, "_source"),
                          select: function(event, ui) { 
                               sizeID=ui.item.id; 
                               }
                      })