flagUpdateCustomer = 0;
$("input#checknames").click(function() {
$.ajax({
url: 'php.scripts/checknames.php',
data: { firstname: $("input#firstname").val(),
lastname: $("input#lastname").val(),
address1: $("input#address1").val(),
address2: $("input#address2").val(),
city: $("input#city").val(),
state: $("input#state").val(),
zip: $("input#zip").val(),
phone: $("input#phone").val(),
email: $("input#email").val(),
cell: $("input#cell").val()
},
type: 'post',
async: false,
success: function (output) {
var obj = $.parseJSON(output);
$("#existcustomers").empty();
for (var iCnt = 0; iCnt < obj.length; iCnt++) {
if (obj[iCnt].firstname == null) { obj[iCnt].firstname = ""; }
if (obj[iCnt].lastname == null) { obj[iCnt].lastname = ""; }
if (obj[iCnt].address1 == null) { obj[iCnt].address1 = ""; }
if (obj[iCnt].address2 == null) { obj[iCnt].address2 = ""; }
if (obj[iCnt].city == null) { obj[iCnt].city = ""; }
if (obj[iCnt].state == null) { obj[iCnt].state = ""; }
if (obj[iCnt].zip == null) { obj[iCnt].zip = ""; }
if (obj[iCnt].phone == null) { obj[iCnt].phone = ""; }
if (obj[iCnt].email == null) { obj[iCnt].email = ""; }
if (obj[iCnt].cell == null) { obj[iCnt].cell = ""; }
var newRow = $("<tr><td>" + obj[iCnt].firstname + "</td><td>" + obj[iCnt].lastname + "</td><td>" + obj[iCnt].address1 + "</td><td>" + obj[iCnt].address2 + "</td><td>" + obj[iCnt].city + "</td><td>" + obj[iCnt].state + "</td><td>" + obj[iCnt].zip + "</td><td>" + obj[iCnt].phone + "</td><td>" + obj[iCnt].email + "</td><td>" + obj[iCnt].cell + "</td></tr>").data('id',obj[iCnt].id);
$("#existcustomers").append(newRow);
}
$("#existcustomers tr").hover(function() { $(this).addClass("highlight"); }, function() { $(this).removeClass("highlight"); });
$("#existcustomers tr").click(function() {
$("#form-main").data('id', $(this).data('id'));
$("input#firstname").val($(this).children(":eq(0)").text());
$("input#lastname").val($(this).children(":eq(1)").text());
$("input#address1").val($(this).children(":eq(2)").text());
$("input#address2").val($(this).children(":eq(3)").text());
$("input#city").val($(this).children(":eq(4)").text());
$("input#state").val($(this).children(":eq(5)").text());
$("input#zip").val($(this).children(":eq(6)").text());
$("input#phone").val($(this).children(":eq(7)").text());
$("input#email").val($(this).children(":eq(8)").text());
$("input#cell").val($(this).children(":eq(9)").text());
$("input#firstname").attr('disabled', 'disabled');
$("input#lastname").attr('disabled', 'disabled');
$("input#address1").attr('disabled', 'disabled');
$("input#address2").attr('disabled', 'disabled');
$("input#city").attr('disabled', 'disabled');
$("input#state").attr('disabled', 'disabled');
$("input#zip").attr('disabled', 'disabled');
$("input#phone").attr('disabled', 'disabled');
$("input#email").attr('disabled', 'disabled');
$("input#cell").attr('disabled', 'disabled');
$("#form-main").data('firstname', $("input#firstname").val());
$("#form-main").data('lastname', $("input#lastname").val());
$("#form-main").data('address1', $("input#address1").val());
$("#form-main").data('address2', $("input#address2").val());
$("#form-main").data('city', $("input#city").val());
$("#form-main").data('state', $("input#state").val());
$("#form-main").data('zip', $("input#zip").val());
$("#form-main").data('phone', $("input#phone").val());
$("#form-main").data('email', $("input#email").val());
$("#form-main").data('cell', $("input#cell").val());
//I am trying to determine how to call .ajax again to set my $_SESSION in PHP
//A recursive call to .ajax does not seem to be a good option here
//I was hoping to see somthing about an onchange or onupdate event in the jQuery docs
//for .data() however I had no such luck. Perhaps could I use just the onchange()
//event of #form-main? Essentially once the data is set in #form-main I have to call
//.ajax again for add.customer.session.php. I am confused right now.
//????????????????
//
//
$('#form-main').css('background-color', 'green');
});
}
});
});
我需要打個電話.ajax()
和呼叫內,設定了$("#existcustomers tr").click()
和裏面點擊處理事情將.ajax()
另一個調用的第一個內,但是我正在尋找另一種最合乎邏輯的方式選項。我現在想知道是否有一些簡單的方法將事件掛接到#form-main
並在.data()
將數據設置爲#form-main
後運行一些代碼。一如既往地感謝您提供任何有關此代碼的建議或評論!避免遞歸調用.ajax();
請勿使用'async:false'。 – SLaks
如果你想避免在對方內部有Ajax調用,你可以使用非匿名函數。這樣,您可以在與調用ajax不同的地方定義回調。 – TonioElGringo
不應該爲ajax成功回調中的所有行添加點擊處理程序。每次發出ajax請求時都會複合點擊處理程序。使用'on()'委託點擊處理程序以允許未來的元素。或者只是將點擊處理程序應用到新行,因爲它們添加了'newRow.click ...' – charlietfl