所以,我有一个函数un JS的问题,如何调用Ajax请求:
function editDataAddress(idPartnerAddress) {
// simplified
var url = "@Url.Action("SelectAddressToEdit", "Partners", idPartnerAddress)"
$.ajax({
url: url,
dataType: 'json',
success: function (data) {
var adresse = data.Address;
var zip = data.Zip;
var locality = data.Locality;
var idLocalite = data.IdLocality;
//alert("locality : "+locality);
$("#ZipSwiss").empty()
$("#ZipSwiss").append($('<option></option>').val(idLocalite).html(zip));
$('#PartnerAdresse_Locality').val(locality)
// This line confirm i don't have another PartnerAdresse_Locality in my page
console.log('ID Test:', $('[id=PartnerAdresse_Locality]').length, $('[id=PartnerAdresse_Locality]').get())
}
});
}
&#13;
<div class="col-md-4" id="divZipSwiss">
@Html.DropDownListFor(model => model.ZipSwiss, ViewBag.localiteList as IEnumerable<SelectListItem>, "Recherche...", new {@class = "form-control", style = "width : 100%;"})
</div>
<div class="col-md-5">
@Html.TextBoxFor(model => model.PartnerAdresse.Locality, new { @class = "form-control" })
</div>
&#13;
问题是,当我调用该函数时,ajax返回所有值,并正确放入4个var(adresse,zip,locality,idlocalite)
当我执行$('#PartnerAdresse_Locality').val(locality)
时,该值在HTML中设置,但不显示:
和HTML:
<input class="form-control" id="PartnerAdresse_Locality" name="PartnerAdresse.Locality" type="text" value="Geneva">
NPA(Zip)没问题,地点值已填满,但未显示
如果我现在发表评论 $("#ZipSwiss").append($('<option</option>').val(idLocalite).html(zip));
本地出现,但当然,由于评论,不再是NPA(zip)。
我真的输了,有人可以帮帮我吗?
答案 0 :(得分:0)
根据我的解释,我通过评论一些代码行来创建解决方案:
对于我的ZipSwiss,我有一个select2 DDL:
$('#ZipSwiss')
.select2({
language: lang,
minimumInputLength: 2,
minimumResultsForSearch: Infinity,
closeOnSelect: true,
ajax: {
url: '@Url.Action("SearchLocalite", "Partners")',
dataType: 'json',
delay: 200,
data: function(params) {
return {
searchTerm: params.term
};
},
processResults: function(data) {
return {
results: $.map(data, function(item) {
return {
id: item.IdLocalite,
text: item.ZipCode,
name: item.Name
}
})
}
},
cache: true
},
templateResult: function(item) {
if (item.loading)
return item.text;
return item.text + " | " + item.name;
},
templateSelection: function (item) {
$("#PartnerAdresse_Locality").val(item.name);
return item.text;
},
escapeMarkup: function(markup) {
return markup;
}
});
更新选择时,行$("#PartnerAdresse_Locality").val(item.name);
始终被调用。这意味着,该值为空。
谢谢^^