jquery closure ajax call, $(this) is null -
i'm new jquery, might seems simple can't see trick :p
i'm using jquery 1.2.6 ( , can't upgrade :/ ) that's why i'm using livequery plugin.
the context update of cart in eshop.
problem don't value want "myid" in update_it.php.
- example 1 (see code below) it's working ! call made , got value in update_it.php "myid". hardcoded, want generic (see example 2).
- example 2 it's not working :( alert working, call working don't value "myid"
here javascript :
$(document).ready( function () { function update_it() { $.ajax({ type: "post", url: "./update_it.php", datatype: "json", data: "myid="+$(this).val(), success: function(msg){ $("div#cart-full").html(msg.fullcart); $("div#cart").html(msg.mincart); }, error : function(xhr, desc, e) { alert("ko - " + xhr.status); } }); } // example 1 $("#mycheckbox_1").livequery("click", update_it); // example 2 $("input:checkbox[id^=mycheckbox_]").each(function(){ $(this).livequery("click", update_it); }); });
thanks !
the example second selection on jquery page involves second set of quotation marks: jquery( "[attribute^='value']" )
try $("input:checkbox[id^='mycheckbox_']").each(function(){
edit:
in case, try "on" keyword in following example instead of livejquery. http://jsfiddle.net/4laqx/2/
$(function () { $(document).ready(function(){ $("[id^='hoverme_']").each(function () { $(this).on("click", listen); }); }); function listen(){ alert($(this).html()); } });
and in case of jquery 1.2.6, click() keyword
$(this).click(dosomethingfunc);
edit
$(document).ready( function () { // example 2 $("input:checkbox[id^=mycheckbox_]").click(update_it); }); function update_it() { $.ajax({ type: "post", url: "./update_it.php", datatype: "json", data: "myid="+$(this).val(), success: function(msg){ $("div#cart-full").html(msg.fullcart); $("div#cart").html(msg.mincart); $("input:checkbox[id^=mycheckbox_]").unbind("click"); $("input:checkbox[id^=mycheckbox_]").bind("click"); }, error : function(xhr, desc, e) { alert("ko - " + xhr.status); } }); }
Comments
Post a Comment