简体   繁体   English

如何使用JQuery从AJAX调用中使用JSON值更新下拉列表

[英]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.

相关问题 如何使用 Jquery Ajax 调用填充下拉列表? - How to fill a DropDown using Jquery Ajax Call? 从jquery中的ajax调用内插json值 - Interpolate json value from ajax call in jquery JSON无法使用jQuery loadJSON插件从ajax调用加载到下拉列表中 - JSON not loading into dropdown list from ajax call using jQuery loadJSON plugin 在 Ajax 中使用 jquery 时如何不从浏览器更新值? - How not update the value from the browser when using jquery in Ajax? 如何使用jquery和ajax更新json中的数据 - How to update data in json using jquery and ajax 使用 Jquery 和 Ajax 和 json 文件从下拉列表中进行动态依赖选择 - Dynamic Dependent Select from dropdown using Jquery and Ajax and json file 在jQuery Ajax调用中为下拉列表添加默认值 - Adding default value to dropdown in jquery ajax call 如何将数据从下拉列表传递到操作方法以使用帖子 jquery ajax 调用保存数据 - How to pass data from dropdown list to action method to save data using post jquery ajax call 如何使用ajax和jquery动态更新1个下拉列表和4个文本字段值 - How to UPDATE 1 dropdown and 4 text filed values dynamically using ajax and jquery 从下拉列表中读取值,并使用jquery和ajax将其发送到同一页面 - read value from dropdown and send it to same page using jquery and ajax
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM