2011-10-06 11 views
9

Voglio fare un avviso che dopo aver fatto clic sul pulsante cancella chiedere did you want delete this? con due opzioni: ok e cancel. Se l'utente fa clic su ok, il valore viene eliminato. Se l'utente fa clic su cancel non cancella il valore.Avviso con diverse opzioni con jQuery

come questo in questo sito:

Stackoverflow confirmation dialog for post deletion vote

come fare questo con jQuery?

risposta

15
<a href="#" id="delete">Delete</a> 

$('#delete').click(function() { 
    if (confirm('Do you want to delete this item?')) { 
     // do delete item 
    } 
}); 

Codice: http://jsfiddle.net/hP2Dz/4/

+0

Questo non funziona, vedi: http://jsfiddle.net/hP2Dz/ –

+0

Sì, ha dimenticato di sfuggire '' 'simbolo in' wan \ 't'. Controlla questo: http://jsfiddle.net/hP2Dz/4/ – Samich

+0

Si può usare da 'direction: rtl;' per allertare e cambiare valore 'ok' e' cancel'? –

1

potrebbe non essere jquery ma è il semplice principio che si potrebbe usare

<html> 
<head> 
<script type="text/javascript"> 
function show_confirm() 
{ 
var r=confirm("vote "); 
if (r==true) 
    { 
    alert("ok delete"); //you can add your jquery here 
    } 
else 
    { 
    alert(" Cancel! dont delete"); //you can add your jquery here 
    } 
} 
</script> 
</head> 
<body> 

<input type="button" onclick="show_confirm()" value="Vote to delete?" /> <!-- can be changed to object binding with jquery--> 

</body> 
</html>Vot 
+1

'var r = confirm ("voto"); if (r == true) '==' if (confirm ("vote")) ' –

2

In JavaScript, questo tipo di scatola è confirm, non alert. confirm restituisce un valore booleano, che indica se l'utente ha risposto positivamente o negativamente al prompt (ovvero facendo clic su OK restituito true, mentre facendo clic su cancel viene restituito false). Questo è applicabile a jQuery, ma anche a JavaScript in senso lato. Si può dire qualcosa di simile:

var shouldDelete = confirm("Do you want to delete?"); 
if (shouldDelete) { 
    // the user wants to delete 
} else { 
    // the user does not want to delete 
} 
2

Se vuoi per lo stile il vostro avviso, controllare questo plugin: jquery-alert-dialogs. È molto facile da usare.

jAlert('This is a custom alert box', 'Alert Dialog'); 

jConfirm('Can you confirm this?', 'Confirmation Dialog', function(r) { 
    jAlert('Confirmed: ' + r, 'Confirmation Results'); 
}); 

jPrompt('Type something:', 'Prefilled value', 'Prompt Dialog', function(r) { 
    if(r) alert('You entered ' + r); 
}); 

UPDATE: il sito oficial è attualmente offline. here is another source

+0

Il sito è attualmente inattivo, ma ho trovato questo: http://labs.abeautifulsite.net/archived/jquery-alerts/demo/ – Kayvar

+0

@Kayvar aggiornerà la mia risposta. Grazie –

Problemi correlati