2011-10-06 11 views
9

Quiero hacer una alerta que después de hacer clic en el botón eliminar preguntar did you want delete this? con dos opciones: ok y cancel. Si el usuario hace clic en ok, se borrará el valor. Si el usuario hace clic en cancel, no elimine el valor.Alerta con varias opciones con jQuery

como este en este sitio:

Stackoverflow confirmation dialog for post deletion vote

cómo hacer esto con jQuery?

Respuesta

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

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

Código: http://jsfiddle.net/hP2Dz/4/

+0

Esto no funciona, ver: http://jsfiddle.net/hP2Dz/ –

+0

Sí, se olvidó de escapar el símbolo ''' en 'wan \' t'. Mira esto: http://jsfiddle.net/hP2Dz/4/ – Samich

+0

¿Se puede usar desde 'direction: rtl;' para alertar y cambiar el valor 'ok' y' cancel'? –

1

no podría ser jQuery, pero es el principio simple que se puede utilizar

<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

En JavaScript, que tipo de caja es confirm, no alert. confirm devuelve un valor booleano, que representa si el usuario respondió positivamente o negativamente al aviso (es decir, al hacer clic en OK se devuelve el resultado true, mientras que al hacer clic en cancel se devuelve false). Esto es aplicable a jQuery, pero también a JavaScript de manera más amplia. Se puede decir algo como:

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

Si desea el estilo de su alerta, comprobar este plugin: jquery-alert-dialogs. Es muy fácil de usar.

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

ACTUALIZACIÓN: el sitio oficial es conectada en este momento. here is another source

+0

El sitio está actualmente fuera de servicio, pero encontré esto: http://labs.abeautifulsite.net/archived/jquery-alerts/demo/ – Kayvar

+0

@Kayvar actualizará mi respuesta. Gracias –

Cuestiones relacionadas