【问题标题】:handling dropdown list处理下拉列表
【发布时间】:2012-03-18 23:05:14
【问题描述】:

我是在 asp.net mvc 上的新开发,甚至更新使用 jquery,所以这是我的问题:我有一些下拉列表,使用另一个下拉列表或文本框的数据动态填充,以及那些数据下拉用于另一个下拉,如链,我现在的问题是下拉有时只有一个答案/选项,如果我尝试选择该选项 jquery 不会激活 $("#anything") .change() 事件,因为我只有一个选项,我该如何处理?

http://imageshack.us/photo/my-images/20/sinttulolp.jpg/

例如,我需要红色圆圈的数据来填充蓝色圆圈,但红色圆圈只有一个选项,如果我点击没有任何反应

代码:

$(document).ready(function() {

            $("#tbClave1").autocomplete({
                minLength: 1,
                source: getClave1,
                select: function(event, ui) {
                    updateClave2(ui.item.value);

                }
            });

            $("#tbClave1").change(function() {
                if ($("#tbClave1").val().length == 4) {
                    updateClave2($("#tbClave1").val());
                }
            });

            $("#ddClave2").change(function() {
                updateClave3($("#tbClave1").val(), $("#ddClave2").val());
                updateNombre($("#tbClave1").val(), $("#ddClave2").val());
                if ($("#ddTipoArticulo").val() == "A" ||
                    $("#ddTipoArticulo").val() == "B" ||
                    $("#ddTipoArticulo").val() == "L") {
                    updatePrecioA($("#tbClave1").val(), $("#ddClave2").val());
                } else { 
                    if ($("#ddTipoArticulo").val() == "N" ||
                         $("#ddTipoArticulo").val() == "H"){
                         updatePrecioB($("#tbClave1").val());
                    }
                }
            });

            $("#ddClave3").change(function() {
                updateClave4($("#tbClave1").val(), $("#ddClave2").val(), $("#ddClave3").val());
            });
        });

编辑:我如何填写下拉列表之一的示例

jQuery

函数 updateClave2(cod) {

            var dd = document.getElementById("ddClave2");
            dd.options.length = 0;
            dd.options[0] = new Option("Espere...");
            dd.selectedIndex = 0;
            dd.disabled = true;
            codigo = cod;
            tipoArt = $("#ddTipoArticulo").val();
            // Control de errores
            $("#ddClave2").ajaxError(function(event, request, settings) {
                dd.options[0] = new Option("No se Cargaron Datos");
            });
            // Obtenemos los datos...
            $.ajax({
                url: '/Home/GetClave2',
                dataType: 'json',
                data: { words: codigo, tipoArticulo: tipoArt },
                success: function(data, textStatus, jqXHR) {
                    $.each(data, function(i, item) {
                        dd.options[i] = new Option(item["clave2"], item["clave2"]);
                    });
                    dd.disabled = false;
                }
            });
        }

控制器

public ActionResult GetClave2(string words, string tipoArticulo){ 列出 mySource = miConexion.Clave2(用户名、密码、单词、tipoArticulo); 返回 Json(mySource); }

【问题讨论】:

  • 为什么不在下拉列表中放置“选择一个”选项并使其成为必需项?这样,它会触发选择更改事件。
  • '因为每次该选项不同时,它并不总是相同的选项,这取决于以前的选项,您在图片中看到的所有三个下拉列表都是动态填充的。
  • 您可以在每个下拉菜单中使用“选择一个”选项以及每个下拉菜单中的不同选项。因此,每次您在 dropdown1 中选择一个选项时,您都会清除 dropdown2 上的选项,添加 Select One 选项,然后添加基于 dropdown1 选择的选项。
  • 哦,对不起,我第一次听不懂你,我是怎么做到的?

标签: c# asp.net-mvc jquery


【解决方案1】:

正如 Alex Mendez 所写,在每个下拉列表中添加一个带有“选择一个”之类的文本的默认项目似乎是您最简单的答案。如果用户重新选择默认的“选择一个选项”,您还需要进行一些处理。要将默认值添加到您的 .Net 下拉列表,请尝试以下代码:

YourListID.Items.Insert(0, new ListItem("Select One", "Select One");

或:

<asp:DropDownList ID="YourListID" runat="server" AppendDataBoundItems="true">
    <asp:ListItem Text="Select One" Value="Select One" Selected="true" />
</asp:DropDownList>

编辑(错过了 MVC) 在 MVC 中,当您绑定初始 ViewData 时,请确保将“Select One”放在最简单的位置:

Dictionary<int, string> dictionaryList = DropDownListItems();
ViewData["ModelData"] = new SelectList(dictionaryList, "Key", "Value")

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-08-28
  • 1970-01-01
  • 1970-01-01
  • 2012-01-25
  • 2021-08-12
  • 2016-03-17
  • 1970-01-01
相关资源
最近更新 更多