[英]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.