jQuery 输入按钮点击事件监听器

编程入门 行业动态 更新时间:2024-10-25 07:33:57
本文介绍了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?

推荐答案

首先,button() 是一个 jQuery ui 函数,用于创建 按钮小部件 与 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!'); });

另一件可能导致您出现问题的事情是,如果您没有等待输入呈现并在输入之前编写代码.jQuery 有 ready 函数,或者它的别名 $(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" />

演示

更多推荐

jQuery 输入按钮点击事件监听器

本文发布于:2023-08-01 18:40:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1270704.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:监听器   按钮   事件   jQuery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!