[英]Calling a jQuery function with JavaScript
我正在使用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函数具有:
my.js
function showName(){
var id = 12;
var a = display(id); //Here I need to call the jQuery function display().
}
如何在JavaScript代码中调用jQuery函数,如何将PHP值返回给jQuery?
我真的认为您需要将这两件事分开:
喜欢:
function display(id){
$.ajax({
type:'POST',
url: 'ajax.php',
data:'id='+id ,
success: anotherFunction; //Ajax response
});
}
然后在您的my.js代码中:
display(2);
function anotherFunction(response){
// Here you do whatever you need to do with the response
$("#response").html(data);
}
请记住,display()将触发您的Ajax调用,并且代码将继续,然后当您收到响应时(可能在几秒钟或ms之后),将调用anotherFunction()。 这就是为什么Ajax是异步的 。 如果需要同步调用,请查看有关jQuery和Ajax技术的文档。
您从分配给Ajax请求中键“成功”的匿名函数中调用showName。 您分配给成功的匿名功能是您的回叫功能。
查看有关Ajax类的文档 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.