2017-07-17 207 views
0

JSP文件Ajax调用返回Object(对象),如何获取值从中

<div class="container"> 
    <table id="headerTable" class="table table-bordered"> 
    <thead> 
    <tr> 
     <th colspan="2">Header</th> 
    </tr> 
    </thead> 
    <tbody> 
    <c:forEach items="${headerList}" var="field"> 
     <tr> 
      <th>${field}</th> 
      <td><input id="${field}" type="text" class="form-control "></td> 
     </tr> 
    </c:forEach>  
    </tbody> 
</table> 

的Javascript

$('#parseBtn').click(function() { 
    var parseMsg = $('#msgText').val(); 
    alert("parse message is " + parseMsg); 
    $.ajax({ 
    type: "GET", 
    url: "/parseMessage", 
    data: { 
     "msg": parseMsg 
    }, 
    success: function(data) { 
     //data format looks like Object {SubsystemChannel: "F", MessageNumber: "200257", DebugQueue: " ", WorkStationNumber: "023", FrontEndNumber: "0000"…} 

     $('#headerTable input').each(function() { 
     var id = $(this).attr('id'); 
     var field = data.id; 
     $(this).val(field); 
     }); 
    } 
    }); 
}); 

我所要做的是,经过$('#headerTable input'),设置它的值(来自数据)。所以,我首先得到每个输入ID,然后使用ID从数据中获得值,但是失败了....你能帮我解决吗?非常感谢你

回答

3

您应该使用Bracket notation而不是点符号来访问属性使用id变量

$('#headerTable input').each(function() { 
    var field = data[$(this).attr('id')]; 
    $(this).val(field); 
}); 
+0

你救我的屁股!并感谢您为我提供支架符号信息!我的代码现在可用。 – Bmegod