Php 用JavaScript调用jQuery函数

Php 用JavaScript调用jQuery函数,php,javascript,jquery,ajax,Php,Javascript,Jquery,Ajax,我正在使用jQuery和JavaScript,我需要在JavaScript代码中调用jQuery函数 我的jQuery代码: function display(id){ $.ajax({ type:'POST', url: 'ajax.php', data:'id='+id , success: function(data){ $("#response").html(data); }

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

我的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 ?
}
我的ajax.php文件包含:

<?php
   session_start();
    ....

   echo "$salary";
?>

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

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


查看。

您通过在Ajax请求中分配给键“success”的匿名函数调用showName。分配给success的匿名函数是回调函数


回顾。

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

  • 触发调用的函数
  • 一个函数,它接收Ajax调用的结果并执行您需要的任何操作
  • 比如:

    然后在my.js代码中:

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

    记住display()将触发Ajax调用,代码将继续,然后当您得到响应时(可能几秒钟或几毫秒后),将调用另一个函数()。这就是为什么Ajax是异步的。如果需要同步调用,请查看有关jQuery和Ajax技术的文档。

    我真的认为您需要将这两种技术分开:

  • 触发调用的函数
  • 一个函数,它接收Ajax调用的结果并执行您需要的任何操作
  • 比如:

    然后在my.js代码中:

    display(2);
    function anotherFunction(response){
        // Here you do whatever you need to do with the response
        $("#response").html(data);
    }
    
    记住display()将触发Ajax调用,代码将继续,然后当您得到响应时(可能几秒钟或几毫秒后),将调用另一个函数()。这就是为什么Ajax是异步的。如果您需要同步调用,请查看有关jQuery和Ajax技术的文档。

    在该行$(“#response”).html(数据);这不是应该是$(“#response”).html(response)吗???在那一行$(“#response”).html(数据);这不是应该是$(“#response”).html(response)吗???
    display(2);
    function anotherFunction(response){
        // Here you do whatever you need to do with the response
        $("#response").html(data);
    }