简体   繁体   English

在jQuery中调用JavaScript函数

[英]Calling a JavaScript function inside jQuery

I am using JavaScript and jQuery. 我正在使用JavaScript和jQuery。 My main file has My.js and Ajax. 我的主文件有My.js和Ajax。

My.js 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()函数?

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>

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. 现在,您可以随时随地调用您的功能。

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. :)如果你想在jQuery中使用结果,那么在另一个文件中定义的函数需要在jQuery之外调用并分配给变量。 Hope that tidbit helps somebody. 希望花絮帮助某人。

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

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