简体   繁体   中英

Button Listener with jQuery doesn't work

So my problem is, I want to run some jQuery-Code when the Button is clicked.

<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.)

"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. When i click the Button, nothing happens.

Putting the Listener in a $(document).ready() function doesn't help.

Thanks in advance for any help!

Why dont you put your function on document.ready ?

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

See Fiddle .

I'm not sure what is $.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. Applying it on jsFiddle gives another error. but keeping your code as regular script works. May be you can explain a bit more. Below is the jsFiddle link

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

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

My Problem was, that the HTML was loaded with AJAX (the .load() function). Although the .load() was written before (!) the listener, the button didn't respond.

The solution is, to write the listener in the callback-function of the .load()-function.

See here:

dynamically add listener to ajax created content in jQuery

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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