[英]Set button focus next to field so that it uses either Enter or Mouse click with jQuery
我希望能夠按Enter鍵或單擊鼠標以在輸入框中提交信息。
現在,當您使用Tab鍵手動更改焦點或使用鼠標單擊時,它將提交信息。 它使用的是jQuery live Click方法。 我找到了將焦點設置為按鈕的解決方案
但我不知道該如何實現,因此我可以使用實時點擊來收聽並執行“或”操作。 鼠標單擊或輸入按鈕。 任何幫助表示贊賞。
示例:可以使用具有焦點的Enter按鈕或單擊鼠標來執行此操作。
$('#theinput').keypress(function(event) {
if (event.keyCode == '13') {
$('#mybutton').click();
event.preventDefault();
}
});
問題是我不知道如何將示例轉換為也使用類似的內容
$('a.button').live('click',function(){
//do stuff here
)};
我可以使用命名函數來做到這一點:
$(document).ready(function(){
var submitter = function() {
// do something
};
$('#theinput').keypress(function(event) {
if (event.keyCode == '13') {
event.preventDefault();
submitter();
}
});
$('a.button').live('click', submitter);
});
這對兩個事件使用相同的功能,而不會觸發另一個不必要的事件,這將導致性能問題。
我認為如果您更改此設置:
$('a.button').live(click,function(){
對此:
$('a.button').live("click",function(){
它應該工作
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.