[英]How to update dropdown with JSON value from AJAX call using JQuery
Hi I am receiving a JSON list in the Ajax success block and I want to assign these values to a dropdown list potentialFailureModeList present on my JSP. 嗨,我在Ajax成功块中收到一个JSON列表,我想将这些值分配给我的JSP上存在的下拉列表potentialFailureModeList。 I am new to JSON, i have tried a lot to findout on the net but did not get anything. 我是JSON的新手,我已经尝试了很多在网上查找的方法,但是没有得到任何结果。 Please help me. 请帮我。 Any useful link will also work. 任何有用的链接也将起作用。
//Code from JSP //来自JSP的代码
<td><select name="fmeaEntityForm[0].potentialFailureMode.id"
id="potentialFailureMode0" onchange="potentialFailureModeText(this)">
<option value="-1"><spring:message code="label.fmea.select.potentialFailureMode" /></option>
<c:forEach items="${potentialFailureModeList}" var="pfm">
<option value="${pfm.id}">${pfm.potentialFailureMode}</option>
</c:forEach>
<option value="0"><spring:message code="label.fmea.select.other" /></option>
</select></td>
//pattern receiving in ajax // Ajax中的模式接收
{"potentialFailureModeList":[{"id":3,"potentialFailureMode":"potentialFailureMode1","remark":"1"},
{"id":4,"potentialFailureMode":"potentialFailureMode2","remark":"2"}]}
//Ajax method function getpotentialFailureModeList(elementIdIndex) { // Ajax方法函数getpotentialFailureModeList(elementIdIndex){
if (-1 != document.getElementById("subSystem"+elementIdIndex+"").value) {
$.ajax({
type: "GET",
url: '/ISO26262/FmeaDocumentPage/potentialFailureModeList',
data: ({subSystemId : $('#subSystem'+elementIdIndex+'').val() }),
success: function(items) {
alert("success");
alert(items);
// to do task
},
error: function (e) {
alert('Error Received: ' + e);
},
});
}
}
json should be in format of json的格式应为
{id: x, value: y}
Then the ajax success function similar to this -- 然后,ajax成功函数类似于此-
success: function (items) {
$.each(items, function (index, item) {
$('#potentialFailureMode0').append($('<option>', {value: item.id, text: item.potentialFailureMode}));
});
Thanks a lot for replying. 非常感谢您的回复。 I got the answer. 我得到了答案。 I am posting here, so that if anyone else need the same. 我在这里发布,以便其他人也需要同样的内容。
function getpotentialFailureModeList(elementIdIndex) {
if (-1 != document.getElementById("subSystem"+elementIdIndex+"").value) {
$.ajax({
type: "GET",
url: '/ISO26262/FmeaDocumentPage/potentialFailureModeList',
data: ({subSystemId : $('#subSystem'+elementIdIndex+'').val() }),
dataType:'json',
success: function(items) {
var list = items;
$.each(list.potentialFailureModeList, function (index, item) {
$('#potentialFailureMode'+elementIdIndex+'').append($('<option>', {value: item.id, text: item.potentialFailureMode}));
})
},
error: function (e) {
alert('Error Received: ' + e);
},
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.