我真的需要我的項目方面的幫助。我的最終目標是捕獲用戶所做的更改,一旦他們選擇確認,將其發佈到SQL以進行更新。我將在項目的後期使用AJAX和PHP,但我認爲JSON將是一個好主意,可以保留用戶所做的所有更改(第一部分)。在JSON中保存臨時數據
我是JSON的新手,我無法將結果放入一個大對象中,當用戶選擇「確定」時,將傳輸到服務器。有人可以幫我編碼嗎?是JSON用於實現該目標(存儲臨時
繼承人是我迄今(只是一個片段)的最佳方式:
HTML
<div class="button" data-info='2' data-id='8-7' onclick=addDeskid(e)></div>
<div class="button" data-info='4' data-id='2-5' onclick=remDeskId()></div>
<div class="button" value="submit">submit</div>
JS
function addDeskId(e){
$adjustment;
userObject = $(this);
userObjectChange = 'CHANGE_SEAT_TO'; //This is what i will use with AJAX and PHP to determine the SQL statement
userObjectID = userObject.attr('data-info'); //This is the unique SQL ID for the object being modified
userObjectDeskID = userObject.attr('data-id'); //This is the attribute for the object being modified
userObjectSeatID = 9-4; //This is what the attribute is being modified to, for the question, ill make it a solid value
var addUserObject = new jsonAddTest(userObjectID, userObjectChange, userObjectDeskID, userObjectSeatID,);
//this is what the function is actually doing on the user side
//$dragObject.attr("data-id",newSeat); //change desk number for new user location
//$dragObject.attr("previousseat", "T");
//var extPass = e;
//moveOrSetupAccountLog(extPass);
}
function remDeskId(){
userObject = $dropObject.find('div.dragTest');
userObjectChange = 'REMOVESEAT'; //This is what i will use with AJAX and PHP to determine the SQL statement
userObjectID = userObject.attr('data-info'); //This is the unique SQL ID for the object being modified
userObjectDeskID = userObject.attr('data-id'); //This is the attribute for the object being modified
userObjectDeskIDVal = 0; //This is what the attribute is being modified to
var remUserObject = new jsonRemTest(userObjectID, userObjectChange, userObjectDeskID, userObjectDeskIDVal);
//this is what the function is actually doing on the user side
//userObject.attr({"data-id":""}); //remove desk number for new user location
//userObject.appendTo('#userPool');
}
//JSON functions test
function jsonRemTest(id, change, name, seat, value){
this.ID = id;
this.ChangeType = change;
this.Name = name;
this.Seat = seat;
this.setTo = value;
userMoves.push(jsonRemTest);
}
function jsonAddTest(id, change, name, desk, seat, previousseat, previousseatnewvalue){
this.ID = id;
this.ChangeType = change;
this.Name = name;
this.Seat = desk;
this.setTo = seat;
this.PreviousSeatValue = previousseat;
this.PreviousSeatNewValue = previousseatnewvalue;
userMoves.push(jsonAddTest);
}
console.log(JSON.stringify(userMoves));
我收到錯誤:userMoves is undefined
。我明白爲什麼會發生這種情況寧,但我不知道如何糾正它。
TL; DR
每次用戶點擊該按鈕時,它產生的陣列。我想將所有數組合併成一個包含所有數組的對象。當用戶點擊提交按鈕時,使用AJAX/PHP將對象發送到服務器。這是做這件事的最好方法嗎?如果是這樣,我如何將JSON函數的輸出結合到一個對象中以準備發送?
在此先感謝
您需要定義'之外userMoves'您功能,以便您可以更新它。我看不到'var userMoves = [];'在你的代碼中。 – Twisty
Twisty明白了!更多的信息在這個http://stackoverflow.com/questions/4862193/javascript-global-variables –
我會給這個鏡頭並報告回來,但你們認爲使用JSON是完成我想要做的最好的方法嗎? – user247326