【问题标题】:Post Input Values With KeyBoard使用键盘发布输入值
【发布时间】:2012-04-08 12:40:46
【问题描述】:

我有一个这样的表格:

<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,但我找不到任何具体的话题。

提前致谢

【问题讨论】:

标签: javascript jquery keyboard-shortcuts


【解决方案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。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多