使用JavaScript调用jQuery函数

时间:2022-09-21 00:00:27

I am using jQuery and JavaScript, I need to call the jQuery function inside my JavaScript code.

我使用的是jQuery和JavaScript,我需要在JavaScript代码中调用jQuery函数。

My jQuery code:

我的jQuery代码:

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

    //Here I need to return the ajax.php salary
    //return ?
}

My ajax.php file has:

我的ajax.php文件有:

<?php
   session_start();
    ....

   echo "$salary";
?>

My JavaScript function has:

我的JavaScript函数有:

my.js

function showName(){
    var id = 12;

    var a = display(id); //Here I need to call the jQuery function display().
}

How do I call the jQuery function inside JavaScript code and how do I return the PHP values to jQuery?

如何在JavaScript代码中调用jQuery函数以及如何将PHP值返回给jQuery?

2 个解决方案

#1


I really think you need to separate both things:

我真的认为你需要把两件事分开:

  1. A function to trigger the Ajax call.
  2. 一个触发Ajax调用的函数。

  3. A function that receive the result from the Ajax call and do whatever you need.
  4. 一个函数,它接收来自Ajax调用的结果并执行您需要的任何操作。

Like:

function display(id){
  $.ajax({
    type:'POST',
    url: 'ajax.php',
    data:'id='+id  ,
    success: anotherFunction; //Ajax response
  });
}

Then in your my.js code:

然后在你的my.js代码中:

display(2);
function anotherFunction(response){
    // Here you do whatever you need to do with the response
    $("#response").html(data);
}

Remember that display() will trigger your Ajax call and the code will continue, then when you get your response (maybe some seconds or ms later) anotherFunction() will be called. That's why Ajax is asynchronous. If you need synchronous calls, check the documentation about jQuery and Ajax technique.

请记住,display()将触发您的Ajax调用,代码将继续,然后当您收到响应时(可能会在几秒或几秒后),将调用anotherFunction()。这就是Ajax异步的原因。如果需要同步调用,请查看有关jQuery和Ajax技术的文档。

#2


You call showName from the anonymous function you assigned to the key 'success' in the Ajax request. The anonymous function you assigned to success is your call back function.

您从分配给Ajax请求中的键“success”的匿名函数中调用showName。您分配给成功的匿名功能是您的回叫功能。

Review the documentation on the Ajax class.

查看Ajax类的文档。

#1


I really think you need to separate both things:

我真的认为你需要把两件事分开:

  1. A function to trigger the Ajax call.
  2. 一个触发Ajax调用的函数。

  3. A function that receive the result from the Ajax call and do whatever you need.
  4. 一个函数,它接收来自Ajax调用的结果并执行您需要的任何操作。

Like:

function display(id){
  $.ajax({
    type:'POST',
    url: 'ajax.php',
    data:'id='+id  ,
    success: anotherFunction; //Ajax response
  });
}

Then in your my.js code:

然后在你的my.js代码中:

display(2);
function anotherFunction(response){
    // Here you do whatever you need to do with the response
    $("#response").html(data);
}

Remember that display() will trigger your Ajax call and the code will continue, then when you get your response (maybe some seconds or ms later) anotherFunction() will be called. That's why Ajax is asynchronous. If you need synchronous calls, check the documentation about jQuery and Ajax technique.

请记住,display()将触发您的Ajax调用,代码将继续,然后当您收到响应时(可能会在几秒或几秒后),将调用anotherFunction()。这就是Ajax异步的原因。如果需要同步调用,请查看有关jQuery和Ajax技术的文档。

#2


You call showName from the anonymous function you assigned to the key 'success' in the Ajax request. The anonymous function you assigned to success is your call back function.

您从分配给Ajax请求中的键“success”的匿名函数中调用showName。您分配给成功的匿名功能是您的回叫功能。

Review the documentation on the Ajax class.

查看Ajax类的文档。