如何禁止第二次点击Google +1按钮

时间:2016-08-30 16:56:32

标签: javascript google-plus-one

我将禁止再次点击Google +1按钮。

<g:plusone callback='click_callback' href="myurl.com"></g:plusone>

我的click_callback功能是:

function click_callback(b) {
  if(b.state == "on") {
    $.ajax({
      type: "POST",
      url: "gp1process.php",
      data: "id=" + id,
      cache: false,
      success: function(a) {
        $("#Hint").html(a);
        remove(id);
        click_refresh()
      }
    })
  }

我可以使用jQuery的one()方法吗?

1 个答案:

答案 0 :(得分:2)

由于我实际上并未发布ajax请求,因此我将$(this).attr("disabled", "disabled")置于ajax调用之前,但您需要在成功函数中使用它。

这样做会在您单击按钮后将disabled =“disabled”属性添加到按钮中。我认为这就是你要找的东西。

$(document).ready(function() {
  $('button').click(click_callback);
});

function click_callback(b) {
  id = $(this).attr('id');
  if (!$(this).attr("disabled")) {
    //the line below should be in your success function
    //i placed it here so you can see the feature work 
    //since i am not really running the ajax
    $(this).attr("disabled", "disabled")
    $.ajax({
      type: "POST",
      url: "gp1process.php",
      data: "id=" + id,
      cache: false,
      success: function(a) {
        $(this).attr("disabled", "disabled")
        $("#Hint").html(a);
        remove(id);
        click_refresh()
      }
    })
  } else {
    //the button is diabled do something else

  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<g:plusone callback='click_callback' href="myurl.com"></g:plusone>
<button id="theID">+1</button>

<div id="hint"></div>