jquery.each 用法和 json

发布于 2024-11-25 21:25:24 字数 1908 浏览 1 评论 0原文

我每个 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"
}]
}

但没有发生预期的名称更改。我非常感谢有关此问题的任何帮助。

提前致谢, 利奥尔

I'm having an issue jquery each. My jsp file includes the following lines:

`<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>
` 

and so on. the relevant Js file includes the following relevant function:

 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;


                }
        });
    }

the relevant Json response looks like this:

 {
"pleyars":[{
    "id":0,
    "name": "mmm",
    "amount":"15"
},
{
    "id":1,
    "name": "mmm2",
    "amount":"152"
}]
}

but the expected name change does not occur. I would really appreciate any assistance on this issue.

Thanks in advance,
Lior

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

强者自强 2024-12-02 21:25:24

而不是

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);
});

if iam 正确地回答了你的问题

instead of

jQuery.each(players, function()         
 {            currPlayer=this;               
 $("#player"+currPlayer+"name").text(currPlayer.name);         
 })

use

$(players).each(function(index,value){
currPlayer = value;
$("#player"+currPlayer+"name").text(currPlayer.name);
});

if iam getting ur question correctly

青衫儰鉨ミ守葔 2024-12-02 21:25:24

尝试:

success: function(JSONResponse) {
   var players=JSONResponse.players;

     jQuery.each(players, function(i,currPlayer)
     {
           $("#player"+currPlayer.id+"name").text(currPlayer.name);
     })

Try:

success: function(JSONResponse) {
   var players=JSONResponse.players;

     jQuery.each(players, function(i,currPlayer)
     {
           $("#player"+currPlayer.id+"name").text(currPlayer.name);
     })
转身泪倾城 2024-12-02 21:25:24

试试这个

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);
                   });
                }
        });
    }

Try this

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);
                   });
                }
        });
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文