2017-07-08 50 views
0

代碼看起來像這樣結束(我的PHP服務器生成此行替換%%變量):onClick事件 - 運行JavaScript之後前一個已在同一onClick事件

<button type="submit" id="buttons" name="add_to_inv" onclick="ServInfo('inv_items','/magagest/index.php','req=add_item&item=%icode% - %idesc%&iqty=%iqty%&iprice=%iprice%&itnum='+trCount('inv_items',false),'','y');ShowFieldTotal('inv_items','iprice','sub-total',2);">+</button>; 

我希望這

ShowFieldTotal('inv_items','iprice','sub-total',2); 

僅當第一個腳本的元素添加到我的網頁上時才運行。基本上,它在表格中添加項目行,並且實際上我希望它執行的操作不能完成,計算每個項目的行數從第一個腳本添加到表格後的總數:

ServInfo('inv_items','/magagest/index.php','req=add_item&item=%icode% - %idesc%&iqty=%iqty%&iprice=%iprice%&itnum='+trCount('inv_items',false),'','y') 
+0

你應該爲ServInfo添加一個額外的參數,以便您可以傳入ShowFieldTotal。然後在ServInfo完成時調用該函數。 JQuery爲大多數/所有函數執行此操作,只有在第一個腳本的元素已添加到我的網頁上時, – ControlAltDel

+0

才能成爲「onLoad」的工作。 https://www.w3schools.com/jsref/event_onload.asp – ArtisticPhoenix

回答

0

像這樣修改你的ServInfo函數。

function ServInfo (args...) { 
    --- your code --- 
    .... 
    .... 

    //call ShowFieldTotal when ServInfo() is finished. 
    ShowFieldTotal(args...); 
} 
+0

我不能這樣做,因爲ServInfo只有從服務器端檢索數據並使用AJAX將其帶到客戶端。 terme服務信息(ServInfo)。代碼ShowFieldTotal也在我的AJAX調用中的另一個js文件中。 –

0

您可以創建另一個函數,它將一個接一個地調用兩個函數,並將該函數綁定到onclick事件。

0

ControlAltDel的回答使它變得簡單和容易。我實際上添加了另一個參數給我的ServInfo,它捕獲一個字符串,它將其轉換爲代碼。基本上,我稱之爲「eval」函數來解析我傳遞給ServInfo的字符串中編寫的代碼。我的新onClick事件是這樣的:

<button type="submit" id="buttons" name="add_to_inv" onclick="ServInfo('inv_items','/magagest/index.php','req=add_item&item=%icode% - %idesc%&iqty=%iqty%&iprice=%iprice%&itnum='+trCount('inv_items',false),'','y','ShowFieldTotal(\'inv_items\',\'iprice\',\'sub-total\',2)');">+</button> 

添加以下到我的ServInfo代碼:

// Execute a string if a user defined one 
     if (exec_string != "") { 
      eval(exec_string); 
     } 

對於好奇,我ServInfo函數現在看起來是這樣的:

// AJAX simple HTTP GET request 
function ServInfo(pop_field_id,web_page,params="",form_id="",append_data_to_output = "",exec_string = "") { 

var sparams = params; 
var swpage = web_page; 
var eobj = document.getElementById(pop_field_id); 

// Get form field values if a form id is specified 
if (form_id != "") { 
    var efrm = document.getElementById(form_id); 
    sparams += "&"+GetDivFields(form_id); 
} 

// If HTML element to populate does not exist, exit 
if (typeof(eobj) == "!undefined" || eobj == null) {return;} 

if (window.XMLHttpRequest) { 
    // IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp = new XMLHttpRequest(); 
} 
else { 
    // IE6- 
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
} 

xmlhttp.onreadystatechange = function() { 
    if (this.readyState == 4 && this.status == 200) { 
     if (append_data_to_output == "y") { 
      document.getElementById(pop_field_id).innerHTML += this.responseText; 
     } 
     if (append_data_to_output == "") { 
      document.getElementById(pop_field_id).innerHTML = this.responseText; 
     } 
     // Execute a string if a user defined one 
     if (exec_string != "") { 
      eval(exec_string); 
     } 
    } 
}; 
// Add the question mark if there is any parameters to pass 
if (sparams != "") {swpage += "?";} 

xmlhttp.open("GET",swpage+sparams,true); 
xmlhttp.send(); 

}