使用JavaScript调用jQuery函数

ven*_*lam 0 javascript php ajax jquery

我使用的是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 ?
}
Run Code Online (Sandbox Code Playgroud)

我的ajax.php文件有:

<?php
   session_start();
    ....

   echo "$salary";
?>
Run Code Online (Sandbox Code Playgroud)

我的JavaScript函数有:

my.js

function showName(){
    var id = 12;

    var a = display(id); //Here I need to call the jQuery function display().
}
Run Code Online (Sandbox Code Playgroud)

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

Ric*_*ega 5

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

  1. 一个触发Ajax调用的函数.
  2. 一个函数,它接收来自Ajax调用的结果并执行您需要的任何操作.

喜欢:

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

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

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

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