代码看起来像这样(我的PHP服务器生成此行替换%%变量):
<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 :(得分:0)
像这样修改你的ServInfo函数。
function ServInfo (args...) {
--- your code ---
....
....
//call ShowFieldTotal when ServInfo() is finished.
ShowFieldTotal(args...);
}
答案 1 :(得分:0)
你可以创建另一个一个接一个地调用这两个函数的函数,并将该函数绑定到onclick事件。
答案 2 :(得分: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();
}