使用KeyBoard发布输入值

时间:2012-04-08 12:40:47

标签: javascript jquery keyboard-shortcuts

我有一个这样的表格:

<form id="surveyForm" method="post" action="submit.php">
    <input type="text"  id="good"  value="0" />
    <input type="text" id="bad" value="1" />    
</form>

我想用“g”和“b”键盘按钮发送值。如果按下“g”按钮,表单将以“0”值提交。如何用jquery做到这一点?我have searched in site但我无法找到任何具体话题。

提前致谢

1 个答案:

答案 0 :(得分:1)

试试这个:

$(document).keypress(function(e) {
    if (e.which == 103) { // 'g' keypress
        $("#bad").attr("disabled", true);
        $("#good").attr("disabled", false);
        $("#surveyForm").submit();
    }
    else if (e.which == 98) { // 'b' keypress
        $("#bad").attr("disabled", false);
        $("#good").attr("disabled", true);
        $("#surveyForm").submit();
    }
});

或者你可以有一个输入,根据按下的键改变值,然后提交你的表格。这将是一个更优雅的IMO。