簡體   English   中英

使用Jquery加載內容以及向其發送變量

[英]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參數附加到要加載的URL,然后在該文件中,您可以將變量插入適當的位置。 沒有重定向或導航到其他頁面,變量將作為內容請求的一部分傳遞。

var x=5;
$("#block").load("file.php?extra_parameter=" + x);

然后,您的PHP文件將通過$_GET '變量訪問x 變量

$x = $_GET["extra_parameter"];

通過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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM