2012-03-30 2 views
0

私はjavascriptを使用してイメージソースを自動的に更新しようとしていますが、何らかの理由で動作しません。画像は私のPHPテキストジェネレータから取得されています(テキストを含む画像を生成します)、テキストは入力ボックスから取得されます。私はそれが自動的に入力ボックス内のテキストを取得したい、その後gen.phpする画像のSRCを変更するには、テキスト= Input_box_text_goes_herejQueryを使ってイメージソースを更新する

ここに私のコードです:?

<html> 
    <head> 
     <title>text generator</title> 
      <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script> 
    </head> 
    <body> 
     <script language=javascript type='text/javascript'> 
      setInterval(function() { 
       var str= document.form.letters.value; 
       $('#logo').src("gen.php?text="+str); 
      }, 10); 
     </script> 
     <img id="logo" name="logo" src="gen.php?text=default" /> 
     <form name="form" action=""> 
      Text: <input type="text" name="letters"/> 
     </form> 
    </body> 
</html> 

任意のヘルプは理解されるであろう。

+1

は10msの画像要求間隔?あなたのブラウザとあなたのサーバーを殺すつもりです – Joseph

答えて

2

$('#logo').src("gen.php?text="+str);から$('#logo').attr("src", "gen.php?text="+str);に変更してください。

1

試してみてください。

<script> 
    $(function(){ 

     //reference the image 
     var img = $('#logo'); 

     //use onkeyup instead of polling 
     $('input[name="letters"]').on('keyUp',function(){ 

      //get the value every keystroke 
      var inputValue = $(this).val(); 

      //apply the values to the image source 
      img.each(function(){ 
       this.src = "gen.php?text="+inputValue; 
      }); 
     }); 
    }); 
</script> 
関連する問題