2016-08-30 9 views
0

Ich verbiete einen zweiten Klick auf Google + 1-Schaltfläche.Wie verbiete ich zweiten Klick auf Google + 1-Schaltfläche

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

Meine click_callback Funktion ist:

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() 
     } 
    }) 
    } 

Kann ich one() Methode von jQuery verwenden?

+3

Ein guter Weg, um zu testen, um zu erfahren, ob es funktioniert – cnorthfield

+1

'jQuery ('click_callback. ') Ein (' Klick', Funktion (e). {Click_callback (e);}); ' –

+1

Möchten Sie einen Doppelklick oder mehr als einen Klick verhindern? – happymacarts

Antwort

2

Da ich nicht wirklich auf eine Ajax-Anfrage posten, habe ich die $(this).attr("disabled", "disabled") vor dem Ajax-Aufruf platziert, aber Sie werden es in Ihrer Erfolgsfunktion wollen.

Was dies tun wird ist das Attribut disabled = "disabled" zu Ihrer Schaltfläche hinzufügen, nachdem Sie darauf klicken. Ich glaube, das ist es, wonach Sie gesucht haben.

$(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

warum haben Sie anstelle von google button verwendet? @happymacarts –

Verwandte Themen