onClick事件-在同一onClick事件中完成上一个javascript之后运行javascript

onClick事件-在同一onClick事件中完成上一个javascript之后运行javascript,javascript,php,Javascript,Php,代码如下所示(我的PHP服务器生成这一行,替换%%变量): 仅当第一个脚本的元素已添加到我的网页上时运行。基本上,它在一个表中添加一个项目行,而我实际上希望它做的事情是我不能做的,当它们的行从第一个脚本添加到表中时,计算每个项目的总数: ServInfo('inv_items','/magagest/index.php','req=add_item&item=%icode% - %idesc%&iqty=%iqty%&iprice=%iprice%&itnum='

代码如下所示(我的PHP服务器生成这一行,替换%%变量):

仅当第一个脚本的元素已添加到我的网页上时运行。基本上,它在一个表中添加一个项目行,而我实际上希望它做的事情是我不能做的,当它们的行从第一个脚本添加到表中时,计算每个项目的总数:

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

像这样修改您的ServInfo函数

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

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

您可以创建另一个函数,该函数一个接一个地调用这两个函数,并将该函数绑定到onclick事件。

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>
// 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();

}
出于好奇,我的ServInfo函数现在看起来如下所示:

<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>
// 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();

}

您应该向ServInfo添加一个额外的参数,以便传入ShowFieldTotal。然后在ServInfo完成后调用该函数。JQuery的大部分/所有功能都是这样做的,比如
onLoad的作业
“仅当第一个脚本的元素添加到我的网页上时”|我不能这样做,因为ServInfo只是从服务器端检索数据,并使用AJAX将其带到客户端。该术语提供信息(ServInfo)的地方。代码ShowFieldTotal也位于与我的AJAX调用不同的js文件中。
// 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();

}