[英]load content with Jquery together with sending variable in to it
我在jquery中有一個變量- x
,我在div
塊中加載一個php部分,我需要將該變量發送給它。
所以 -
$(document).on('click','#link',function () {
var x=5;
$("#block").load("file.php");
});
結果我仍然在同一頁面。我只需要有variable x
。
我想使用GET方法或XMLHttpRequest,但我不想要另一個頁面,我只需要在塊中加載一個php部分。
一種解決方案是將數據作為第二個參數發送:
$(document).on('click', '#link', function() {
$("#block").load("file.php", {
x: 5
}, function(res) {
//your callback
});
});
參考
通過Get參數傳遞變量值,如下所示:
$("#block").load("file.php?variable_name=" + x);
派對遲到了,但我還是要發布它:試試Jquery.ajax(),它會允許你把你的數據發送給POST
$.ajax({
type: "POST",
url: url,
data: data, //The variables you want to send through
complete: function(obj, status) {
//Here you can take the response from the request and load
//it into the current page
},
dataType: dataType //What you expect the format of the returned
//data will be, usually html or json
});
參考: http : //api.jquery.com/jquery.ajax/
如果這看起來太麻煩了,那么還有一個包裝器: http : //api.jquery.com/jquery.post/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.