我正在尝试从 json 响应在我的应用程序的下拉列表(选择)中添加选项。
这是我的 JSON 响应:
{"Value":[{"Sub_Bhav_Copy_Name":"All","Sub_Bhav_id":"0"},{"Sub_Bhav_Copy_Name":"COC Prices","Sub_Bhav_id":"2"},{"Sub_Bhav_Copy_Name":"Cotton Price","Sub_Bhav_id":"4"},{"Sub_Bhav_Copy_Name":"Kapas Prices","Sub_Bhav_id":"1"},{"Sub_Bhav_Copy_Name":"Yarn Prices","Sub_Bhav_id":"3"}]}
这是我的js代码:
function callcottonVarUrl(url) {
$.ajax({
url: url,
type : "GET",
dataType : "json",
contentType: "application/json",
async : false,
success : function(msg) {
jsonRespCtnVar = msg.Value;
alert(jsonRespCtnVar);
$("#CategorySelection option").remove();
$.each(jsonRespFav, function(index, value) {
cottonVarName = value.Sub_Bhav_Copy_Name;
cottonVarId = value.Sub_Bhav_id;
$jsonRespCtnVar = '<option value="'+cottonVarId+'">'+cottonVarName+'</option>';
$('#CategorySelection').append($jsonRespCtnVar);
});
},
error : function(msg) {
alert ("error occured");
}
});
}
这是 .html :
<div id="searchbg">
<span>SEARCH COTTON</span>
<p id="selectcontainer">
<b></b>
<select id="CategorySelection" data-native-menu="true" name="" onChange="changetextbox()">
<!-- <option value="">Please Select Cotton Type</option> -->
</select></p>
<div id="search">
<input name="" id="searchBox" type="text"
placeholder="Enter Commodity"> <a href="#"
class="greenbtn" id="button" type="submit"><span class=" sprite"></span></a>
</div>
但是当我对此进行测试时,下拉菜单中没有生成任何选项。不知道原因。有帮助吗?