I have this code that was given to me as a solution earlier. What I would like to do is to make it so when the user clicks the ENTER key (13) that this then makes an input button on the form appear to be clicked. Is this possible with JQuery. Could I make it lo开发者_如何学Pythonok like the user clicked the button and have the button change just for a very short time?
$('#target').keypress(function(event) {
if (event.which == '13') {
//Do something with the enter press
}
Use trigger method
$('#target').keypress(function(event) {
if (event.which == '13') {
$(<YOUR_BUTTON_SELECTOR>).trigger('click');
}
});
If by "input button" you mean an <input type="submit">
element, or a <button>
element, then no you can't.
You'd need to create a custom button.
And then if you do a custom button, it will depend on how you're changing its appearance during clicks.
If you're using mousedown/mouseup
, then you'll need to trigger those events.
$(selector).mousedown();
$(selector).mouseup();
EDIT:
If you want to style a custom button, or a <div>
that contains your <input>
button, you can use .css()
to add a color:
element.css('background','blue');
setTimeout(function(){
element.css('background','none');
}, 1000);
The setTimeout
will give a delay of 1 second (1000 milliseconds) before the background is reset.
Alternatively, you could add/remove a class, which is probably better.
element.addClass('active');
setTimeout(function(){
element.removeClass('active');
}, 1000);
EDIT: It looks like there's more support than I thought for coloring browser generated UI buttons. So you can use the same techniques above to color the buttons directly.
精彩评论