3

我有这样的表格:

<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 做到这一点?我在网站上搜索过,但找不到任何特定主题。

提前致谢

4

1 回答 1

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。

于 2012-04-08T12:47:04.063 回答