簡體   English   中英

MVC @ Html.DropDownList用於更改javascript中的顯示值

[英]MVC @Html.DropDownListFor change displayed value in javascript

使用MVC 4,我試圖根據另一個下拉列表的操作來更改@Html.DropDownListFor的視圖中的顯示值。 在下面的示例中,當用戶在狀態列表中選擇任何值時,我希望@Html.DropDownListFor顯示索引的203值,即US。 這個怎么做?

//in the controller

ViewBag.statelist = new SelectList(db.states, "state_abbr", "state_name");

ViewBag.countrylist = new SelectList(db.countries, "country_id", "country_name", -1);

//in the View

<div class="form-group">
    @Html.LabelFor(model => model.states)
    @Html.DropDownList("ddlstate", ViewBag.statelist as SelectList, Model.rrq_sec1_waddr_stprov, htmlAttributes: new { @onchange="StateChange()" })
</div>

<div class="form-group">
   @Html.LabelFor(model => model.country)
   @Html.DropDownListFor(model => model.country, ViewBag.countrylist as SelectList, "Select Country", htmlAttributes: new { @id="ddlcountry" })
</div>

//javascript
function StateChange() {

    var state = document.getElementById("ddlstate").value;
    $("#ddlcountry").val = 203;
};

如何獲得#ddlcountry索引= 203(在視圖中顯示“ US”,然后狀態下拉列表更改)?

這可能有助於u使用此jQuery函數在第二個下拉列表中選擇值,在第一個下拉列表的onchange事件中進行選擇。

 $('#ddlstate').change(function () {
 $("#ddlcountry").val( $('#ddlstate').val());
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM