Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post Input Values With KeyBoard

I have a form like this:

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

I want to send value with "g" and "b" keyboard buttons. If "g" button is pressed, form submits with "0" value. How can do this with jquery? I have searched in site but I cant find any specific topic.

Thanks in advance

like image 265
jasmine Avatar asked Mar 20 '26 18:03

jasmine


1 Answers

Try this:

$(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();
    }
});

Alternatively you could have one input which you change the value of depending on the key pressed and then submit your form. That would be a little more elegant IMO.

like image 178
Rory McCrossan Avatar answered Mar 22 '26 08:03

Rory McCrossan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!