2017-07-08 49 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(); 

}