這里的方法不起作用。
這是我的 cshtml:
<div class="form-group col-md-4">
<label for="inputState">City</label>
<select id="City" class="form-control basic" asp-for="City" asp-items="@(new SelectList(ViewBag.Cities,"Id","ZoneName"))" readonly="@(Model.IsView ? true : false)">
<option value="0" selected>Select</option>
</select>
</div>
查詢
$("#CustomerName").change(function () {
var CustomerName = $("#CustomerName").val();
$.ajax({
url: "../Ticket/FetchCustomerAddress",
data: { userId: CustomerName },
type: "Get",
success: function (data) {
alert(data.city);
//debugger;
$('#City').val(data.city);
},
error: function (err) {
//console.error(err)
//alert("Internal server error.");
}
});
我正在用城市填充串列。我想從串列中自動選擇城市data.city
uj5u.com熱心網友回復:
ASP 為您在客戶端的輸入生成新的 Id,
所以你選擇 html 看起來像
<select ID="City" class="form-control basic" asp-for="City" asp-items="@(new SelectList(ViewBag.Cities,"Id","ZoneName"))" readonly="@(Model.IsView ? true : false)">
<option value="0" selected>Select</option>
</select>
為此生成的 id 是 : City.ClientID,所以為了訪問這個選定的,你的 js 代碼應該看起來像
...
success: function (data) {
alert(data.city);
//debugger;
$("#<%=City.ClientID%>").val(data.city);
},
...
請注意,$("#<%=City.ClientID%>") -->新生成的 id 將列印在選擇器中
uj5u.com熱心網友回復:
你可以試試這個
$("#city option[value=data.city]").attr('selected', 'selected');
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/394600.html
標籤:javascript html 查询 asp.net-mvc
下一篇:具有水平滾動的多個粘性部分
