简体   繁体   English

执行完先前的代码后,如何在javascript函数中调用另一个javascript函数?

[英]How to call another javascript function within a javascript function after previous codes have been executed?

I got this seemingly simple problem, yet I cannot find the correct way of execution. 我遇到了一个看似简单的问题,但是我找不到正确的执行方式。

I have 2 select tags, the first one is COLOR and the second one is SIZE. 我有2个选择标签,第一个是COLOR,第二个是SIZE。 The second one is dependent on the first one. 第二个依赖于第一个。

To retrieve the price of the products (which is from the database), I intend to call a function within another function. 为了检索产品价格(来自数据库),我打算在另一个函数中调用一个函数。 The code stated below. 下面说明的代码。

    function checksizes(id)
    {
    var color=document.getElementById('colors').value;
    $("#sizediv").show();
    $("#sizediv").load('sizes.php?id='+id+'&color='+color);
    var size = document.getElementById('size2').value;
    alert(id+size+color);
    confirmprice2(id,size,color);
    }

    function confirmprice2(id,size)
    {
    $("#price").show();
    $("#price").load('price.php?id='+id+'&size='+size);
    }

The alert is to check whether the invoked values to be passed on the next function are correct. 警报是检查要在下一个函数上传递的调用值是否正确。

Code is working, but returning different results. 代码正在运行,但是返回不同的结果。 It seemed that the function checksizes() passes values which are from the previous select (from the size). 似乎函数checksizes()传递的值来自上一个选择(来自大小)。 It calls the second function even before it finishes executing this: $("#sizediv").load('sizes.php?id='+id+'&color='+color); 它甚至在完成执行之前就调用第二个函数:$(“#sizeiv”)。load('sizes.php?id ='+ id +'&color ='+ color);

Help is much appreciated. 非常感谢您的帮助。 Thank you! 谢谢!

The load function is an asynchronous function. 加载功能是异步功能。 When the function is "finneshed" does not mean that the result is loaded. 当函数“完成”时,并不意味着将结果加载。 You need the completed Callback for that. 为此,您需要完成的回调。 See http://api.jquery.com/load/ 参见http://api.jquery.com/load/

function checksizes(id)
{
    var color=document.getElementById('colors').value;
    $("#sizediv").show();
    $("#sizediv").load('sizes.php?id='+id+'&color='+color, function(){
        var size = document.getElementById('size2').value;
        alert(id+size+color);
        confirmprice2(id,size,color);
    });
}

function confirmprice2(id,size)
{
    $("#price").show();
    $("#price").load('price.php?id='+id+'&size='+size, function(){
        alert('price is loaded');
    });
}

That's because .load() is asynchronous. 这是因为.load()是异步的。 It basically forks off another thread to continue its work while the calling code continues what it was doing. 它基本上派生出另一个线程来继续工作,而调用代码继续执行其工作。 If you have code which needs to happen in response to .load() then it needs to be placed in a callback function, not after the function. 如果您有需要响应.load()代码,则需要将其放在回调函数中,而不是在函数之后。 Something like this: 像这样:

function checksizes(id) {
    var color=document.getElementById('colors').value;
    $("#sizediv").show();
    $("#sizediv").load('sizes.php?id='+id+'&color='+color, function () {
        var size = document.getElementById('size2').value;
        alert(id+size+color);
        confirmprice2(id,size,color);
    });
}

Passing this function as a parameter to .load() will invoke this function after .load() completes. 将此函数作为参数传递给.load()将在.load()完成后调用此函数。

As from the jQuery documentation, $.load does indeed not finish before it returns. 从jQuery文档开始, $.load实际上在返回之前并没有完成。 You should use the callback function: 您应该使用回调函数:

$( "#result" ).load( "ajax/test.html", function() {
  alert( "Load was performed." );
});

http://api.jquery.com/load/ http://api.jquery.com/load/

In your case, everything in the checksizes function under the load function call should be placed in the callback. 在您的情况下, checksizes load函数调用下的checksizes函数中的所有内容放在回调中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM