Javascript jQuery 输入按钮点击事件监听器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10508319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery input button click event listener
提问by Bob
Brand new to jQuery. I was trying to set up an event listener for the following control on my page which when clicked would display an alert:
全新的 jQuery。我试图在我的页面上为以下控件设置一个事件侦听器,单击该控件时会显示警报:
<input type="button" id="filter" name="filter" value="Filter" />
But it didn't work.
但它没有用。
$("#filter").button().click(function(){...});
How do you create an event listener for a input button control with jQuery?
如何使用 jQuery 为输入按钮控件创建事件侦听器?
回答by gdoron is supporting Monica
First thing first, button()
is a jQuery ui function to create a button widgetwhich has nothing to do with jQuery core, it just styles the button.
So if you want to use the widget add jQuery ui's javascript and CSS files or alternatively remove it, like this:
首先,button()
是一个 jQuery ui 函数,用于创建一个与 jQuery 核心无关的按钮小部件,它只是为按钮设置样式。
因此,如果您想使用该小部件,请添加 jQuery ui 的 javascript 和 CSS 文件,或者将其删除,如下所示:
$("#filter").click(function(){
alert('clicked!');
});
Another thing that might have caused you the problem is if you didn't wait for the input to be rendered and wrote the code before the input. jQuery has the ready function, or it's alias $(func)
which execute the callback once the DOM is ready.
Usage:
可能导致您出现问题的另一件事是,如果您没有等待呈现输入并在输入之前编写代码。jQuery 有ready 函数,或者它的别名$(func)
,一旦 DOM 准备好就执行回调。
用法:
$(function(){
$("#filter").click(function(){
alert('clicked!');
});
});
So even if the order is this it will work:
因此,即使订单是这样,它也会起作用:
$(function(){
$("#filter").click(function(){
alert('clicked!');
});
});
<input type="button" id="filter" name="filter" value="Filter" />
回答by Danilo Valente
$("#filter").click(function(){
//Put your code here
});
回答by CHAN
More on gdoron's answer, it can also be done this way:
更多关于 gdoron 的回答,也可以这样做:
$(window).on("click", "#filter", function() {
alert('clicked!');
});
without the need to place them all into $(function(){...})
无需将它们全部放入 $(function(){...})