jQuery的输入按钮点击事件监听器 [英] jQuery input button click event listener

查看:553
本文介绍了jQuery的输入按钮点击事件监听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全新jQuery的。
我试图建立一个事件侦听器在我的网页下面的控制,点击会显示报警时:

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:

<input type="button" id="filter" name="filter" value="Filter" />

但它没有工作。

$("#filter").button().click(function(){...});

你如何创建一个事件侦听器使用jQuery一个输入按钮控制?

How do you create an event listener for a input button control with jQuery?

推荐答案

第一件事,第一,按钮()是一个jQuery UI函数来创建的button窗口小部件这无关与jQuery核心,它只是风格的按钮。结果
所以,如果你想要使用的小部件添加jQuery UI的的JavaScript和CSS文件或可选择地删除它,是这样的:

First thing first, button() is a jQuery ui function to create a button widget which 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:

$("#filter").click(function(){
    alert('clicked!');
});

这可能造成你的问题的另一件事是,如果你没有等待输入被渲染和输入以前写的code。 jQuery的拥有的 功能齐全 的,或者它的别名 $(FUNC)其中执行回调一旦DOM准备好了。结果
用法:

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:

$(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" />

DEMO

这篇关于jQuery的输入按钮点击事件监听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆