【问题标题】:Update JSP Page Label Value Using AJAX使用 AJAX 更新 JSP 页面标签值
【发布时间】:2013-11-14 05:37:39
【问题描述】:

我有一个如下的 JSP 页面,我想在 5000 毫秒的间隔内根据数据库值快速更新标签“lblscore”。

<script type="text/javascript">  
            $(document).ready(function(){   

                setInterval(function() {
                     $.ajax({  
                        url:'TimerAjax?userid='+document.getElementById("uid").value,
                        type:'post', 
                        dataType: 'json',  
                        success: function(data) {  

                            $('#lblscore').val(data.score);  

                        }  
                    });  
                }, 5000);
            });  
        </script>  
 <div class="bodycontent">

                <table>
                    <tr>
                        <td>Current Score</td><td><label id="lblscore"></label></td>
                    </tr>
                </table>
</div>

我的 servlet 如下所示,

response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {

            ResultSet rs = db.selectQuery("select * from tbl_score where userid = '2'");
            JSONObject json = new JSONObject();
            while (rs.next()) {
                json.put("score", rs.getString("currentscore"));


            }

            out.print(json);

        } catch (Exception e) {
            e.printStackTrace();
        }

但是标签永远不会更新,请任何人在这里建议或更正我的错误?

【问题讨论】:

    标签: java jquery ajax jsp


    【解决方案1】:

    &lt;label/&gt; 没有value 属性。使用html() 而不是val()

    setInterval(function() {
       $.ajax({  
         url:'TimerAjax?userid='+document.getElementById("uid").value,
         type:'post', 
         dataType: 'json',  
         success: function(data) { 
                $('#lblscore').html(data.score); //Here use html()
               }  
          });  
     }, 5000);
    

    【讨论】:

      猜你喜欢
      • 2016-04-19
      • 1970-01-01
      • 2019-05-13
      • 1970-01-01
      • 2016-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多