2016-08-30 87 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 –

相關問題