【问题标题】:dropdownlist item text is always failing in if condition如果条件,下拉列表项文本总是失败
【发布时间】:2017-10-02 14:27:09
【问题描述】:

我想显示一个带有 texbox 的 div,只有当下拉列表中选择的项目文本等于“其他”时

从下面,我只想在选择下拉列表项文本“其他”时显示一个 div 'divOtherIncident'。如果选择了其他内容,则 弹出窗口应该出现(这个位工作正常)

我这里有两个问题。

1) 对于包括“其他”在内的所有选定项目文本,它总是进入其他条件。我可以看到警报显示其他文本,但它永远不会进入 if 条件 2) div 从不显示或隐藏,

谁能帮忙。

<div class="form-group">
            @Html.LabelFor(model => model.SelectedIncident, htmlAttributes: new { @class = "control-label col-md-3" })
            <div class="col-md-9">
                @Html.DropDownListFor(model => model.SelectedIncident, new SelectList(string.Empty, "Value", "Text"), new { data_toggle = "popover", @class = "form-control", @id = "IncidentId" })
                @Html.ValidationMessageFor(model => model.SelectedIncident, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group hidden" id="divOtherIncident">
            @Html.LabelFor(model => model.OtherIncident, htmlAttributes: new { @class = "control-label col-md-3" })
            <div class="col-md-9">
                @Html.EditorFor(model => model.OtherIncident, new { htmlAttributes = new { @class = "form-control", @id = "tbOtherIncident"} })             
            </div>
        </div>

        $("#IncidentId").change(function () {
                var ddlOIValue = $("#IncidentId option:selected").text();
                alert(ddlOIValue); //This displays the text 'Other' but it never goes into if condition below
                if (ddlOIValue == "Other") {    
                    $('#divOtherIncident').show();  

                }
                else {  
                        $('#divOtherIncident').hide();      
                        $("#tbOtherIncident").val("");  
                        $.ajax({
                        type: 'GET',
                        url: '@Url.Action("GetActionDetails")',
                        dataType: 'html',
                        data: { id: $("#IncidentId").val() },
                        success: function (data) {                          
                            $('[data-toggle="popover"]').popover({
                                html: true,
                                content: data
                            });
                        },
                        error: function (ex) {
                            alert('Failed to retrieve action details' + ex);
                        }
                    });
                    return false;                       
                }
            });

【问题讨论】:

    标签: jquery asp.net-mvc twitter-bootstrap


    【解决方案1】:

    我不知道您的下拉菜单是什么样子,但如果下拉文本和值不一样,那么您的条件总是会失败。

    所以尝试获取选定的值而不是文本。即:

    var ddlOIValue = $('#IncidentId').val(); 
    

    var ddlOIValue = $("#IncidentId option:selected").val();  
    

    看看这个fiddle的区别

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多