我每个人都遇到问题jquery。我的jsp文件包含以下几行:
`<script type="text/javascript" src="js/monopolyAL.js"></script>
...
<script type="text/javascript">
$(this).ready(function() {
// window.setTimeout(myTimerTask, 1000); // for a single timeout event
window.setInterval(myTimerTask, 3000); // for periodical timeout events
myTimerTask();
});
</script>
...
<table title="Player Information" id="playersDataTable">
<tr>
<td>Player's Icon</td>
<td>Player's Name</td>
<td>Player's money</td>
</tr>
<tr>
<td id="player0Icon">bla</td>
<td id="player0name">bla</td>
<td id="player0money">bla</td>
</tr>
`
等等。相关的Js文件包括以下相关功能:
function myTimerTask() {
jQuery.ajax({
data: "", // no data - can be ommitted
url: "MonopolyAjaxServlet",
timeout: 2000,
error: function() {
console.log("Failed to send ajax");
},
success: function(JSONResponse) {
var players=JSONResponse.players;
var currPlayer;
jQuery.each(players, function()
{
currPlayer=this;
$("#player"+currPlayer+"name").text(currPlayer.name);
})
//"ver "+ver+" dice "+dice;
}
});
}
相关的Json响应如下所示:
{
"pleyars":[{
"id":0,
"name": "mmm",
"amount":"15"
},
{
"id":1,
"name": "mmm2",
"amount":"152"
}]
}
但不会发生预期的名称更改。我真的很感激在这个问题上的任何帮助。
提前致谢, 利奥尔
答案 0 :(得分:1)
而不是
jQuery.each(players, function()
{ currPlayer=this;
$("#player"+currPlayer+"name").text(currPlayer.name);
})
使用
$(players).each(function(index,value){
currPlayer = value;
$("#player"+currPlayer+"name").text(currPlayer.name);
});
如果我正确地提出你的问题
答案 1 :(得分:0)
尝试:
success: function(JSONResponse) {
var players=JSONResponse.players;
jQuery.each(players, function(i,currPlayer)
{
$("#player"+currPlayer.id+"name").text(currPlayer.name);
})
答案 2 :(得分:0)
试试这个
function myTimerTask() {
jQuery.ajax({
data: "", // no data - can be ommitted
url: "MonopolyAjaxServlet",
timeout: 2000,
error: function() {
console.log("Failed to send ajax");
},
success: function(JSONResponse) {
var players=JSONResponse.players;
jQuery.each(players, function(i, curPlayer)
{
$("#player"+curPlayer.id+"name").text(curPlayer.name);
});
}
});
}