0

$.postこんにちは、Javascript のメソッドを使用して、スペース バーが押されたときにライブ テキスト フィールドから別のスクリプトにデータを送信するページを作成しようとしています。index.phpとして知られるページのデータを、save.phpという別のファイルを介して投稿したいと考えています。txt_atextfield の値を post 変数の値にしたいtext。どうすればいいですか?以下はこれまでの私のコードです...

<html>   
<head>
    <script type="text/javascript">
        function event_a() {
            if (event.keyCode == 13) { 
               $(document).ready(function(){
                   txt=$document.getElementsByName('txt_a')[0].value;
                   $.post("save.php",{text:txt});
               };
            };
        }
    </script>
</head>
<body>
    <form method="POST" name="" action="">
        <input name="txt_a" type="text" id="txt_a" onkeypress="event_a()"/>
    </form>
</body>
</html>

ありがとう

4

3 に答える 3

1

これで始められるはずです:

$(function(){

    $('#txt_a').keyup(function(e){

        if(e.which === 13) {

            $.post('save.php',{text: $(this).val()}, function(data){
                //do something with the response
            });
        }

    });

});

そしてsave.phpで:

$text = isset($_POST['text']) ? $_POST['text'] : null;
于 2013-01-28T15:37:06.163 に答える
0

あるファイルから別のファイルに変数を渡すのはなぜですか? $_SESSION を使用して、設定したデータ/変数を複数のページで保存および取得します。

于 2013-01-28T15:42:49.120 に答える
0

JavaScript 関数は次のようになります。

function event_a(event) { // event needs to be defined as arg to use.
  if (event.keyCode == 13) { // ready is used to trigger code when the page is ready, not needed here
    var txt=$('txt_a').val(); // assuming you are using jQuery, you can access the value like this
    $.post("save.php",{text:txt});
  }
}
于 2013-01-28T15:36:35.687 に答える