Saturday 15 February 2014

ajax - jQuery click handler not firing for all elements -


Several buttons based on JSON have a php (information from the database in which I am using MySQL) code below I want to work with buttons, but for some reason, click handler is said only when I click on the first button in the list. Any idea why the handler does not fire for all the buttons and how can I fix it? $ (document) .ready (function () {$ .ajax ({url: "file.php", data: "id = 1", data type: "jason", type : "Post", success: function (data) {$ .ee (data, function (index, element) {var classBtns = ""; $ each (return_error), function (i, d) { Class_button = "& lt; span class = '' 'id =' classBtn 'data-date ='" + + "'data-time ='" + element. Time + "'> press & lt; / span & Gt; "classBtns + =" & lt; p class = 'goDates'> "+ d + class_button +" ";}); $ ('div') (" Click "," $$ (this) ".data ('date'));})}}}}};

In addition to this, Data is passing from. What is the best way to keep data in click here in Helldaler? I prefer it to not be visible in HTML if possible, but I did not know that this data for Headler Why is it convenient if I have clicked the click handler over $ at any time, then the alert is sometimes called when a button is clicked.

first id in an HTML page < Strong> must be unique.

Next you must delegate

This selector $ ('# classBtn') Only how many elements will be available with that ID, will get the first example.

Instead of using class ..

I think if you have already set up the class for the element, then id Get rid of

and change this piece of code

  $ ("# classBtn"). ("Click", ".button" function (on "click", function () {  

to

  $ (document) {  

You can delegate events to stable ancestor instead of document for better performance.

The way you are binding events, they can be bound many times in the same element. Take this piece of code outside the Ajax request

  $ (document) .ready (function () {$ ajax ({url: "file.php", data: "id = 1", data Type: "post", success: function (data) {$ .each (data, function (index, element) {var classBtns = ""; $ .each (return_are (element.date), function (I, d) {class_button = "& lt; span class = '' button 'id =' classBtn 'data-date ='" + + D + "data-time = '" element + time + "' & gt; Press & lt; / span & gt; "classBtns + =" & lt; p class = 'goDates'> "+ d + class_button +" & lt; / p & gt; ";}); $ (' Di V ') (classBtns) attached;});}}); $ (document) .on ("click", ".button" function) {alert ("clicked" + $ (this) .data (' date '));}); });    

No comments:

Post a Comment