2016-08-30 88 views
0

我要禁止第二次点击Google +1按钮。如何禁止第二次点击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()方法?

+3

学习的一个好方法是测试它是否工作 – cnorthfield

+1

'的jQuery( 'click_callback。 ')一个(' 点击',功能(E){click_callback(E);} );' –

+1

你想防止双击或多点击? – happymacarts

回答

2

因为我实际上没有发布到ajax请求,所以我在之前放置了ajax调用,但是你会希望它在你的成功函数中。

这将做什么是添加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>

+0

为什么你使用而不是谷歌按钮? @happymacarts –

相关问题