duanjiwu0324 2009-01-25 05:12
浏览 69
已采纳

使用JavaScript调用jQuery函数

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

My jQuery code:

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:

<?php
   session_start();
    ....

   echo "$salary";
?>

My JavaScript function has:

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?

  • 写回答

2条回答 默认 最新

  • dongshuo8756 2009-01-25 08:15
    关注

    I really think you need to separate both things:

    1. A function to trigger the Ajax call.
    2. A function that receive the result from the Ajax call and do whatever you need.

    Like:

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

    Then in your my.js code:

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误