简体   繁体   English

jQuery的按钮侦听器不起作用

[英]Button Listener with jQuery doesn't work

So my problem is, I want to run some jQuery-Code when the Button is clicked. 所以我的问题是,我想在单击Button时运行一些jQuery代码。

<input type="button" name="appointments_view" value="Alle Termine anzeigen" />

Here is my jQuery-Class (For testing, the Listener should print out something on the console.) 这是我的jQuery-Class(为进行测试,侦听器应在控制台上打印出一些内容。)

"use strict";

$.Class("View", {
init : function() {

    $("[name='appointments_view']").click(function() {
        console.log("appointments_view");
    });

}
});

If i run it in the browser, Firebug isn't giving me any errors. 如果我在浏览器中运行它,Firebug不会给我任何错误。 When i click the Button, nothing happens. 当我单击按钮时,什么也没有发生。

Putting the Listener in a $(document).ready() function doesn't help. 将侦听器放在$(document).ready()函数中没有帮助。

Thanks in advance for any help! 在此先感谢您的帮助!

Why dont you put your function on document.ready ? 为什么不将函数放在document.ready

$(function () {
    $("[name='appointments_view']").click(function() {
        console.log("appointments_view");
    });
});

See Fiddle . 参见小提琴

I'm not sure what is $.Class(..)?? 我不确定$ .Class(..)是什么?? Are you trying to write a plugin or something. 您是否要编写插件或其他东西。 Tried running your code in Visual Studio and gave me the error object doesn't support this property. 尝试在Visual Studio中运行您的代码,并给我错误对象不支持此属性。 Applying it on jsFiddle gives another error. 将其应用于jsFiddle会产生另一个错误。 but keeping your code as regular script works. 但是将您的代码保留为常规脚本即可。 May be you can explain a bit more. 也许您可以解释更多。 Below is the jsFiddle link 以下是jsFiddle链接

$("[name='appointments_view']").click(function(){
//your code goes here.
});

http://jsfiddle.net/ruchin/RsT6D/1/ http://jsfiddle.net/ruchin/RsT6D/1/

My Problem was, that the HTML was loaded with AJAX (the .load() function). 我的问题是HTML加载了AJAX(.load()函数)。 Although the .load() was written before (!) the listener, the button didn't respond. 尽管.load()是在侦听器(!)之前编写的,但是按钮没有响应。

The solution is, to write the listener in the callback-function of the .load()-function. 解决的方法是在.load()函数的回调函数中编写侦听器。

See here: 看这里:

dynamically add listener to ajax created content in jQuery 动态地将侦听器添加到jQuery中Ajax创建的内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM