javascript - How can I determine/use $(this) in js callback script -
I am using Rail and jQuery, so that an AJAX call can be initiated by clicking on a link. I set up my application.js file to look like the proposed one and it works great. The problem I am facing is how can I use $ (this) in my saying .. to update the .js.erb file I clicked to represent the link? I do not have to assign an ID to every one, then compile that ID in the callback script.
Edit To give a simple example of doing something like this I'm trying to (and more easy to understand): If a user clicks on a link, then removes that element from the list, the controller has to handle the callback, and the callback (here in question ) Will remove the element On principle I clicked, so the callback only $ (this) .fadeOut (in delete.js.erb); This is the reason I want to use $ (this) so that I do not assign an ID to every element (which will be the end of the world, more verbose markup)
Application.js
jQuery.ajaxSetup ({'Already before': function (xhr) {xhr.setRequestHeader ("Accept", "Text / Javascript, application / javascript, text / html ")}} function_ajax_request (url, data, callback, type, method) {if (jQuery.isFunction (data)) {callback = data; data = {};} returns jQuery Ajax ({type: method, url: url, data: d {Return: _ajax_request (url, data, callback, type, 'put');}, {call: return, Delete_: function (url, data, callback, type) {return_araj_safe (url, data, callback, type, 'delete');}}); jquery.fn.submitWithAjax = function () {this.unbind ('submit' False); submit submit (function () {$ .post (this.action, $ (this). Serialize (), blank, "script"); return returned;}) return it; }; // get if & lt; Acronym title = "javascript" & gt; JS & lt; / Acronym & gt; Send data through Enabled jQuery.fn.getWithAjax = function () {this.unbind ('click', false); This.click (function () {$ .get ($ (this) .attr ("href"), $ (this). Serialize (), null, "script"); return returned;}} returned;}; // Post via "Javascript"> JS enabled jQuery.fn.postWithAjax = function () {this.unbind ('click', false); Click (function () {$ .post ($ (this) .attr ("href"), $ (this) .serialize (), blank, "script"); Return return;}) return it;}; JQuery .fn.putWithAjax = function () {this.unbind ('click', wrong); This.click (function () {$ .put ($ (this) .attr ("href"), $ (this). Serialize (), Null, "script"); return returned;}} returned;}; jquery.fn.deleteWithAjax = function () {this.removeAttr ('onclick'); This.unbind ('click', wrong); This .click (function () {$ .delete_ ($ (this) .attr ("href"), $ (this). Serialize (), null, "script"); return returned;}} returned;}; // This "ajaxify" link function will be ajaxLinks () {$ ('.jaxform'). SubmitWithAjax (); $ ('A.get') getWithAjax (); $ ('A.post') postWithAjax () ; $ ('A.put') putWithAjax () .; $ ('A.delete') deleteWithAjax () .; }
show.html.erb
& lt;% = link_to 'link title ', Article_path (a:: emotion = & gt; paragraph :: emotion [' neutral ']),: class => 'Put'% & gt;
The combination of two things will call update.js.erb in the rail, the code for that file is used as a callback of Ajax ($. In this case)
update.js.erb
// user feedback $ ("# notice ") .html ('& lt;% = flash [: notice]%> gt;'); // Update background color $ (this or e.target) .attr ("color", "red");
already handles this
issue with jQuery Event Properties:
$ ("a"). Click (function (e) {e.preventDefault (); $ ("# foo") .FadeIn (3000, function () {$ (E.target) .text ("Foo loaded");})}} ;
Note how can I referencing the main link back through my event
. This is the case of any such event, which is well controlled. Just give them unique names like e2
, e3
, etc. No more need to write any more var item = $ (this)
this
line to keep track of three events back.
Online demo:
Comments
Post a Comment