I'm seemed to be confused here with on and off. With Setting the event and then turning them off during an $.Ajax function
In my $(document).ready, I do the bind, or On. I was using .click
$(document).on("change", "#Shipper_VendorID", function () {
load_rateAPICodes();
});
$(document).on("change", "#Shipper_RateAPICode", function () {
load_RateAPIDetails();
});
When I had declared the function here, the function was firing during unload so I removed the function
beforeSend: function () {
$(document).off("change", $ddl_vendorID);
$(document).off("change", $ddl_serviceID);
},
Here I rebind the function, but it fires the function I'm trying to bind.
complete: function () {
$(document).on("change", $ddl_vendorID, load_rateAPICodes());
$(document).on("change", $ddl_serviceID, load_RateAPIDetails());
}
I guess I don't understand why on and off fires the function when I just want to ignore the bind.
This is the whole function in case your wondering what I'm doing here.
You click on a radio button item in a group and I fetch the vendor and service, and then run another $.Ajax to get the service maximum parameters.
But if the vendor is different, I have to reload the services that match that vendor, and then select the service which isn't working well for me.
I just too much going on here.
function run_packageSelect(_packageID) {
var $txt_orderID = $("#Order_ID"),<br />
$ddl_vendorID = $("#Shipper_VendorID"),
$ddl_serviceID = $("#Shipper_RateAPICode"),
$js_packageAdd = $("#js_packageAdd"),
_dataVendorID = $("#Shipper_VendorID option:selected").attr("value"),
_dataVendorAttr = $ddl_serviceID.attr("data-vendor");
$js_packageAdd.collapse('hide');
$.ajax({
type: "POST",
cache: true,
dataType: "json",
global: true,
url: '/Ajax/Json_Shipment_Order_Package_Fetch',
data: { Package_ID: parseInt(_packageID, 10) },
beforeSend: function () {
$(document).off("change", $ddl_vendorID);
$(document).off("change", $ddl_serviceID);<br />
},
error: function (response) {<br />
alert("http code: " + response.status + " Error: Json_Shipment_Order_Package_Fetch - data: " + response);
},
success: function (data) {
$ddl_vendorID.val(data.Vendor_ID);
if (parseInt(_dataVendorID, 10) !== parseInt(_dataVendorAttr, 10)) {
$.ajax({
type: "POST",
cache: true,
dataType: "json",
url: '/Ajax/Json_Shipment_Load_Vendor_Services',
global: true,
data: { ShipperID: parseInt(data.Vendor_ID, 10) },
error: function (response) {
alert("http code: " + response.status + " Error: Json_Order_Shipment_Load_Services - data: " + response);
},
success: function (data) {
var markup = "<option value=''> " + data.length + " services(s) found --</option>";
for (var x = 0; x < data.length; x++) {
if (data.Value == data.Service_ID) {
markup += "<option value=" + data[x].Value + " selected>" + data[x].Text + "</option>";
}
else {
markup += "<option value=" + data[x].Value + ">" + data[x].Text + "</option>";
}
}
$("#Shipper_RateAPICode").html(markup).show();
},
complete: function () {
$ddl_serviceID.val(data.Service_ID).change();
}
});
}
else {
$ddl_serviceID.val(data.Service_ID).change();
}
},
complete: function () {
$(document).on("change", $ddl_vendorID, load_rateAPICodes());
$(document).on("change", $ddl_serviceID, load_RateAPIDetails());<br />
}
});
}
If it ain't broke don't fix it
|