在jQuery中调用JavaScript函数

时间:2021-10-10 23:57:35

I am using JavaScript and jQuery. My main file has My.js and Ajax.

我正在使用JavaScript和jQuery。我的主文件有My.js和Ajax。

My.js

function build_one(){
    alert("inside build_one");
}

My main file

<script type="text/javascript">

    ..

    // Here I want to make call function defined in My.js build_one()
    ..

    // Here is the Ajax call

    $.ajax({
        type:'POST',
        url: 'ajax.php',
        data:'id='+id  ,
        success: function(data){
            $("#response").html(data);
        }
    });

    ...

</script>

How do I make the build_one() function call before the Ajax function?

如何在Ajax函数之前调用build_one()函数?

3 个解决方案

#1


This should work:

这应该工作:

<script type="text/javascript" src="My.js"></script>
<script type="text/javascript">

    build_one();

    $.ajax({
            type:'POST',
            url: 'ajax.php',
            data:'id='+id  ,
            success: function(data){
                $("#response").html(data);
            }
         });
</script>

#2


First you have to import your file before calling the function using the following

首先,您必须在使用以下函数调用函数之前导入文件

<script type="text/javascript" src="My.js"></script>

Now you can call your function where ever you want.

现在,您可以随时随地调用您的功能。

#3


I figured out my question. :) The function that's defined in another file needs to be called outside of jQuery and assigned to a variable if you want to use the results inside jQuery. Hope that tidbit helps somebody.

我想出了我的问题。 :)如果你想在jQuery中使用结果,那么在另一个文件中定义的函数需要在jQuery之外调用并分配给变量。希望花絮帮助某人。

#1


This should work:

这应该工作:

<script type="text/javascript" src="My.js"></script>
<script type="text/javascript">

    build_one();

    $.ajax({
            type:'POST',
            url: 'ajax.php',
            data:'id='+id  ,
            success: function(data){
                $("#response").html(data);
            }
         });
</script>

#2


First you have to import your file before calling the function using the following

首先,您必须在使用以下函数调用函数之前导入文件

<script type="text/javascript" src="My.js"></script>

Now you can call your function where ever you want.

现在,您可以随时随地调用您的功能。

#3


I figured out my question. :) The function that's defined in another file needs to be called outside of jQuery and assigned to a variable if you want to use the results inside jQuery. Hope that tidbit helps somebody.

我想出了我的问题。 :)如果你想在jQuery中使用结果,那么在另一个文件中定义的函数需要在jQuery之外调用并分配给变量。希望花絮帮助某人。