をフォームを送信:は)(キープレスを使用してjQueryを使って私がこれまで持って何
私は次が含まれているPHPのフォームがあります。
<input type="submit" id="0" name="0" value="This button">
<input type="submit" id="1" name="1" value="This button">
<input type="hidden" id="response" name="response" value="150">
問題:
を私は、ユーザーが送信するボタンに応じて、応答値をname = 1またはname = 0のいずれかと一緒に送信したいとします。ただし、これはキーボードの文字AとSを使用して行う必要があります。ユーザーが文字Aを押すと値0を送信し、Sを押すと値1を送信する必要があります。
のjQueryコード:
$(document).ready(function()
{
// listens for any navigation keypress activity
$(document).keypress(function(e)
{
switch(e.which)
{
// user presses the "a"
case 97: submitViaKeypress("0");
break;
// user presses the "s" key
case 115: submitViaKeypress("1");
break;
}
});
});
// shows a given element and hides all others
function submitViaKeypress(element_id)
{
var response = $('#response').attr('value');
$.ajax({
type: "POST",
url: "initiate.php",
data: "response=" + response + "&" + element_id + "=" + element_id
});
}
は目標:。
「2つのPOST変数(応答および0または1のいずれか)を受信initiate.php、それが聞こえる何
私はあなたのコードを試しましたが、キーAまたはSのどちらにも反応しません。コードが正しいように見えますが、何が問題なのかはっきりしません。 – kexxcream
"data:"を開始する行にセミコロンを付けました。今すぐ試してみてください。 – fet
まだ動作しません。 – kexxcream