【问题标题】:How to send jquery post request using button如何使用按钮发送 jquery post 请求
【发布时间】:2016-02-08 09:27:49
【问题描述】:

我正在尝试使用 jquery 发送发布请求。这是我要发送的请求

$.post("https://api.steampowered.com/IEconService/DeclineTradeOffer/v1/", { 
    key: "1234567891011121314151617181920", 
    tradeofferid: "$offerid" 
}).done(function(data) {
    alert("Data Loaded: " + data);
});

在我的 php 中,我有 $offerid ,并且密钥总是相同的。

我想在点击按钮时发送它,所以我在我的 php 中创建了按钮

$offerid = $value2['tradeofferid'];

echo '<button id="cancel-offer"> Cancel offer </button>';       

如何将按钮与 jquery 请求连接并将 $offerid 发送到请求?

【问题讨论】:

    标签: php jquery post


    【解决方案1】:

    您只需要通过其id 属性选择按钮,然后添加一个点击处理程序:

    $('#cancel-offer').click(function() {
        $.post("https://api.steampowered.com/IEconService/DeclineTradeOffer/v1/", { 
            key: "1234567891011121314151617181920", 
            tradeofferid: "<?= $offerid ?>" 
        }).done(function(data) {   
            alert("Data Loaded: " + data);
        });
    });
    

    我建议您快速阅读jQuery documentation。它提供了很好的参考,还让您了解 jQuery 的功能和不具备的功能。

    【讨论】:

      【解决方案2】:

      像这样修改你的代码:

      php:

      echo '<button id="cancel-offer" data-id="'.$offerid.'"> Cancel offer </button>';
      

      js:

      $('#cancel-offer').click(function(){
      $.post( "https://api.steampowered.com/IEconService/DeclineTradeOffer/v1/", { key: "1234567891011121314151617181920", tradeofferid: $(this).attr('data-id') },function( data ) {
      alert( "Data Loaded: " + data );
          });
      });
      

      【讨论】:

        猜你喜欢
        • 2021-06-13
        • 1970-01-01
        • 1970-01-01
        • 2014-06-09
        • 2018-04-27
        • 2019-05-13
        • 2017-05-17
        • 1970-01-01
        相关资源
        最近更新 更多