Input 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 a value using "g" and "b" buttons. If the "g" button is pressed, the form is submitted with a "0" value. How can I do this with jquery? I have searched the site but I cannot find any specific topic.

Thank you in advance

+3


source to share


1 answer


Try the following:

$(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 can have one input that you change the value based on the key pressed and then submit the form. That would be a little more elegant IMO.

+1


source







All Articles