Reputation: 954
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);
}
});
});
});
Upvotes: 0
Views: 192
Reputation: 887767
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.
Upvotes: 1
Reputation: 887767
$(document.getElementsByName("ShippingPostalCode")).change(/*your function*/);
Upvotes: 0