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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With