如何使用按钮发送jquery发布请求

时间:2016-02-08 09:27:49

标签: php jquery post

我正在尝试使用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发送给请求?

2 个答案:

答案 0 :(得分:3)

您只需按其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是什么和不具备的。

答案 1 :(得分:0)

像这样修改你的代码:

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 );
    });
});
相关问题