发送特定数据到ajax

时间:2018-12-30 23:07:44

标签: javascript jquery html ajax

我要向ajax发布变量数据,这是我的 html代码

<tr>
                <td class="text-right">Payment ID:</td>
                <td>variable</td> // variable that i want to post
              </tr>

这是我的Java脚本代码

var ajax_call = function() {
      $.ajax({
                    type:"post",
                    url:"request-handler.php",
                    data:{
                    
                    data:"Payment ID"
                    
                    }
                });


};

var interval = 5000; // where X is your every X minutes

setInterval(ajax_call, interval);

现在我要在ajaxpost中的td标签之间发布变量

2 个答案:

答案 0 :(得分:2)

在该单元格上设置一个id,以便您的js易于阅读,然后只需使用jQuery即可访问文本。

var ajax_call = function() {
  $.ajax({
    type: "post",
    url: "request-handler.php",
    data: {
      paymentId: $('#payment-id').text();
      // access this on the php server using: $_POST['paymentId'];
    }
  });
};

var interval = 5000; // where X is your every X minutes

setInterval(ajax_call, interval);
<tr>
  <td class="text-right">Payment ID:</td>
  <td id="payment-id">variable</td>
  <!-- variable that i want to post -->
</tr>

答案 1 :(得分:0)

<tr id="rowID">
  <td class="text-right">Payment ID:</td>
  <td>variable</td> // variable that i want to post
</tr>

以及您的javascript

var ajax_call = function() {
      $.ajax({
           type:"post",
           url:"request-handler.php",
           data:{
                    var Row = document.getElementById("rowID");
var Cells = Row.getElementsByTagName("td");
                    data:Cells[1].innerText

                    }
                });


};

编辑:您可以直接使用var cells = document。 getElementsByTagName (“ td”);也一样或设置ID和单元格  <tr > <td class="text-right">Payment ID:</td> <td id="columnID">variable</td> // variable that i want to post </tr>

data:{document.getElementById("columnID")}