0

Ajax を使用して 2 つのフィールドを更新/更新するテスト コードをいくつか作成しました。それはうまく機能し、物事をきちんと保つために、Ajax.jsというファイルを作成しました

すべてのフィールドがある HTML ページで、このファイルを呼び出して、2 つの変数を更新させたいと思います。

私は次のことをしました:

私のHTMLページで:

  <--- included the meta lines needed for the Ajax file ---->
  <--- included: my jQuery includes needed ---------->
  <---- my link to the Ajax.js file -------------->
 <script src="Ajax.js"></script>

 <----- made the call this way, I am not sure is right ------->
 <!----- Ajax Update Fields Function ----->
 <script>
   jQuery('nav').Ajax();
 </script>

呼び出しは機能せず、フィールドを更新しません。実際の Ajax.js コードは次のとおりです。

// JavaScript Document
<script src="http://code.jquery.com/jquery-1.10.2.js"></script> 


$(function()
{
  setInterval(function()
   {
$.get("ajax_v00.html",function(data,textStatus, jqXHR)
 {
  var temperature=$(data).filter('#variable1').text()
  var time=$(data).filter('#variable2').text()
  $('#variable1').text(temperature)
  $('#variable2').text(time)
 })
 .fail(function(jqxhr,textStatus,errorThrown) //Callback failed
  {
   $('#errors').text("Errors:" + textStatus + " " + errorThrown)
  })
 .done(function() //Callback succeeded
  {
   $('#errors').text('Errors:');
  })
   },1000);
})

どうすればこれを適切に呼び出すことができますか? うまくいけば、私は自分自身を正しく説明しましたか?

4

2 に答える 2

2

これをあなたのhtmlページの頭に入れてください...

<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript" src="Ajax.js"></script>

からスクリプト タグを削除しますAjax.js

削除jQuery('nav').Ajax(); する必要はありません。Ajax()存在しないという jQuery 関数を探しています。のコードはAjax.js、ページがロードされたときにすでに実行されていますdocument.ready

$(function() {
    setInterval(function() {
        $.get("ajax_v00.html",function(data,textStatus, jqXHR) {
            var temperature=$(data).filter('#variable1').text();
            var time=$(data).filter('#variable2').text();
            $('#variable1').text(temperature);
            $('#variable2').text(time);
        })
        .fail(function(jqxhr,textStatus,errorThrown) {
            $('#errors').text("Errors:" + textStatus + " " + errorThrown);
        })
        .done(function() {
            $('#errors').text('Errors:');
        })
    },1000);
});
于 2013-09-09T16:16:20.817 に答える
0

このスクリプトを使用してください。

   <script>
    $(document).ready(function() {
                      jQuery('nav').Ajax();});
  </script>
于 2013-09-09T16:07:41.927 に答える