【发布时间】:2012-04-01 19:04:12
【问题描述】:
我目前所拥有的:
我在 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
});
}
目标:
“initiate.php 接收两个 POST 变量(响应和 0 或 1)。
【问题讨论】: