[英]How to hide and show div in asp.net mvc 5 using dropdownlist change event
我已经研究过,我正在尝试使用dropdownlist change事件在我的mvc 5项目中隐藏和显示div,幸运的是,我在网上找到了此代码,但是它似乎对我没有用,如果有人可以指出我的位置,我将不胜感激我在犯错误。 提前致谢。
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$(document).ready(function () {
$("#CountryID").change(function () {
if ($(this).val() == "Ghana") {
$("#showStateLga").show();
$("#showStateLgaText").hide();
} else {
$("#showStateLga").hide();
$("#showStateLgaText").show();
}
});
});
});
</script>
下拉列表控件:
<div class="form-group">
@Html.LabelFor(model => model.CountryID, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.CountryID, (IEnumerable<SelectListItem>)ViewBag.cCountryList, "---Select---", new {@class = "form-control" })
@Html.ValidationMessageFor(model => model.CountryID, "", new { @class = "text-danger" })
</div>
</div>
Div控制:
<div id="showStateLga" style="display: none">
<div class="form-group">
@Html.LabelFor(model => model.notState, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.notState, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.notState, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.notCity, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.notCity, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.notCity, "", new { @class = "text-danger" })
</div>
</div>
</div>
渲染结果:
下拉列表:
<div class="form-group">
<label class="control-label col-md-2" for="CountryID">Country:</label>
<div class="col-md-10">
<select class="form-control" data-val="true" data-val-number="The field Country: must be a number." data-val-required="Select country" id="CountryID" name="CountryID"><option value="">---Select---</option>
<option value="1">Afghanistan</option>
<option value="2">Ghana</option>
<option value="3">Albania</option>
<option value="4">Algeria</option>
</select>
<span class="field-validation-valid text-danger" data-valmsg-for="CountryID" data-valmsg-replace="true"></span>
</div>
</div>
DIV1:
<div id="showStateLga" style="display:block">
<div class="form-group">
<label class="control-label col-md-2" for="UserStateList">State:</label>
<div class="col-md-10">
<select class="form-control" id="State" name="State"><option value="">---Select State---</option>
<option value="1">Abia State</option>
<option value="2">Adamawa State</option>
<option value="3">Akwa Ibom State</option>
</select>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2" for="UserCity">City:</label>
<div class="col-md-10">
<select id="lga" name="lga" class="form-control" required>
<option value="">---Select LGA---</option>
</select>
</div>
</div>
</div>
DIV2:
<div id="showStateLgaText" style="display:none">
<div class="form-group">
<label class="control-label col-md-2" for="notNigeriaState">State:</label>
<div class="col-md-10">
<input class="form-control text-box single-line" data-val="true" data-val-required="Enter state" id="notNigeriaState" name="notNigeriaState" type="text" value="" />
<span class="field-validation-valid text-danger" data-valmsg-for="notNigeriaState" data-valmsg-replace="true"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-2" for="notNigeriaCity">City:</label>
<div class="col-md-10">
<input class="form-control text-box single-line" data-val="true" data-val-required="Enter city" id="notNigeriaCity" name="notNigeriaCity" type="text" value="" />
<span class="field-validation-valid text-danger" data-valmsg-for="notNigeriaCity" data-valmsg-replace="true"></span>
</div>
</div>
</div>
我发现了问题。 那是$(“#CountryID”)的值是CountryID而不是CountryName 。
$(function () {
$(document).ready(function() {
$("#CountryID").change(function () {
if ($(this).val() != "Ghana") { // It doesn't work over here.
$("#showStateLga").show();
} else {
$("#showStateLga").hide();
}
});
});
});
有2种解决方法。 第一
if ($(this).val() != "2") { // Replace the match text to CountryID.
要么
if ($(this).find(':selected').text() != "Ghana") { // Replace .val() to .find(':selected').text().
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.