tags:

views:

106

answers:

2

Hi,

On one section of my website, I ask my customers for their postal code and country. Shipping rates are loaded from another page once they enter these information and click on calculate link. Now, I want to remove the click button and once these information entered, I want users see loading image and load data immediately. I was wondering if that is possble and if there are any examples.

$(document).ready(function() { 
$("#link").click(function() { 
     $.ajax({
   type: "GET",
   data: { PROCESS: "UPS", WEIGHT: "<%=Session("TotalWeight")%>", POSTALCODE: $(document.getElementsByName("ShippingPostalCode")).val(), COUNTRY: $(document.getElementsByName("ShippingCountry")).val() },
   url: "content/checkout/step1/index.cs.asp", 
         success: function(output) { 
         $("#sonuc").html(output);
         $("#sonuc").css("display", "block");
   }
     }); 
 }); 
});

Quick update! I allowed my customers to store addresses for future use. If they have an address stored, they can copy and paste the address with one click. Here is the code for that:

function StoredData(){
 $.get("includes/ajaxfunctions.asp?Process=checkout1", { QUERY: $(document.getElementsByName("CUSTOMERDETAILNAME")).val() }, function(data){
  $("div.StoredData").html(data);
 });
}


$(document).ready(function() { 
 $("input[name=ShippingPostalCode]").livequery("change", function() { 
     $.ajax({
   type: "GET",
   data: { PROCESS: "UPS", WEIGHT: "<%=Session("TotalWeight")%>", POSTALCODE: $(document.getElementsByName("ShippingPostalCode")).val(), COUNTRY: $(document.getElementsByName("ShippingCountry")).val() },
   url: "content/checkout/step1/index.cs.asp", 
         success: function(output) { 
          $("#sonuc").html(output);
   }
     }); 
 }); 
});
A: 
$(document.getElementsByName("ShippingPostalCode")).change(/*your function*/);
SLaks
thats great thank you so much. I need to figure how to add a loader now.
Efe
hello again,Just tested it, this method is partially working for me. If customers have a stored address and copy/paste that information than its not loading shipping rates. I am so sorry I should have mentioned that feature at the beginning but I did not think they were related.
Efe
+1  A: 

I assume that your StoredData div contains these inputs.

By writing $("div.StoredData").html(data), you are replacing the inputs that have the event handlers registered on them with new input elements that don't. You need to reregister your event handlers every time you do that.

The simplest way to do that is to use jQuery's LiveQuery plugin, like this:

$("input[name=ShippingPostalCode]").livequery("change", /*your function*/);

Run this once on load, and everything should work fine.

Alternatively, you could reregister your handlers after the AJAX callback, after running $("div.StoredData").html(data).

Finally, you could also change you AJAX request (ajaxfunctions.asp?Process=checkout1) to return the data as JSON instead of the HTML containing the data, and update the values of the existing elements yourself. This is the fastest solution.

SLaks
A good example of using event delegation via the LiveQuery plugin.
Peter
Hi SLaks,Still not working. I read about livequery plugin at the link you provided me. It should have been solved my problem like you've suggested, but still same issue there. I just edited my original post with change and livequery.
Efe
Your AJAX request is probably returning incorrect HTML. Using FireBug, make sure that the correct elements still exist after the update, and that they have the same names.
SLaks