JQuery Show Default Div Only

时间:2015-06-01 10:55:40

标签: javascript jquery razor

我想基于下拉列表默认选择值显示Div,但不能正常工作,它只能在我下拉时更改所选值时显示和隐藏。我尝试使用加载功能,但它会显示所有Div。有人可以指导我吗?

以下是我的代码:

<script>

    $('#dropDown').load(function () {
        $('.drop-down-show-hide').hide();
        $('#' + this.value).show();
    });

    $('#dropDown').change(function () {
        $('.drop-down-show-hide').hide();
        $('#' + this.value).show();
    });

</script>

View.cshtml:

<div id="Single_Line" class="drop-down-show-hide">
    <br />
    <div class="editor-label">
        @Html.LabelFor(model => model.MaxChar)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.MaxChar)
        @Html.ValidationMessageFor(model => model.Qext_Text)
    </div>
</div>

<div id="Multiple_Line" class="drop-down-show-hide">
    <br />
    <div class="editor-label">
        @Html.LabelFor(model => model.NumberOfLines)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.NumberOfLines)
        @Html.ValidationMessageFor(model => model.NumberOfLines)
    </div>
</div>

下拉列表:

@Html.DropDownListFor(model => model.Question_Type, new SelectList(
                          new List<Object>{

                               new { value ="Single Line" , text = "Single_Line" },
                               new { value ="Multiple Line" , text = "Multiple_Line" },                         
                            },
                          "text",
                          "value",
                      0), new { @id = "dropDown" })

1 个答案:

答案 0 :(得分:1)

使用下面的代码。在 <script> // Add this line of code if you want to show dive after DOM load. $(window).load(function(){ $('.drop-down-show-hide').hide(); $('#'+$('#dropDown').val()).show(); }); </script> 函数中添加<script> $(document).ready(function(){ $('.drop-down-show-hide').hide(); $('#'+$('#dropDown').val()).show(); $('#dropDown').load(function() { $('.drop-down-show-hide').hide(); $('#' + this.value).show(); }); $('#dropDown').change(function() { $('.drop-down-show-hide').hide(); $('#' + this.value).show(); }); }); </script>

$("#model").find("option[id='50']").hide();

第二个选项

var test = ("{"g":"776","f":"88876","hit":"true","testvalue":"this is the value i want to capture"}");
var capture =