2011-06-29 33 views
120

Para una forma sencilla, con una alerta que le pregunta si los campos se llenen correctamente, necesito una función que hace esto:Formulario JavaScript Enviar - confirmar o cancelar el cuadro de diálogo Presentación

  • Muestra un cuadro de alerta cuando el botón se hace clic con dos opciones:

    • Si se hace clic en "Aceptar", se envía el formulario
    • Si se hace clic en cancelar, el cuadro de alerta se cierra y la forma se puede ajustar y volver a presentarse

Creo que una confirmación de JavaScript funcionaría, pero no puedo entender cómo.

El código que tengo ahora es:

<script> 
function show_alert() { 
    alert("xxxxxx"); 
} 
</script> 
<form> 
    <input type="image" src="xxx" border="0" name="submit" onclick="show_alert();" 
     alt="PayPal - The safer, easier way to pay online!" value="Submit"> 
</form> 

Respuesta

265

Un simple JavaScript en línea confirmar sería suficiente:

<form onsubmit="return confirm('Do you really want to submit the form?');"> 

No hay necesidad de un función externa a menos que usted está haciendo validación, lo que se puede hacer algo como este:

<script> 
function validate(form) { 

    // validation code here ... 


    if(!valid) { 
     alert('Please correct the errors in the form!'); 
     return false; 
    } 
    else { 
     return confirm('Do you really want to submit the form?'); 
    } 
} 
</script> 
<form onsubmit="return validate(this);"> 
+0

cuando agrega '' 'onClick =" this.form.submit(); this.disabled = true; this.value = 'Enviando ...'; "' '' al botón de enviar, la ventana emergente no aparece en Chrome mientras funciona en Firefox – shintaroid

+1

@shintaroid Si tiene otra pregunta, por favor, haga clic en el botón [Ask Question] (// stackoverflow.com/questions/ask). –

23
function show_alert() { 
    if(confirm("Do you really want to do this?")) 
    document.forms[0].submit(); 
    else 
    return false; 
} 
+1

Este código sub con la primera forma en el documento. – user254153

17

usted podría utilizar la función JS confirmar.

<form onSubmit="if(!confirm('Is the form filled out correctly?')){return false;}"> 
    <input type="submit" /> 
</form> 

http://jsfiddle.net/jasongennaro/DBHEz/

+21

'onsubmit =" return confirm ('¿El formulario se completó correctamente?'); "' Sería mucho más simple, y el resultado sería el mismo. – Sk8erPeter

2
<form onsubmit="return confirm('Do you really want to submit the form?');"> 
1

bien, sólo cambiar su código a algo como esto:

<script> 
function submit() { 
    return confirm('Do you really want to submit the form?'); 
} 
</script> 

<form onsubmit="return submit(this);"> 
    <input type="image" src="xxx" border="0" name="submit" onclick="show_alert();" 
     alt="PayPal - The safer, easier way to pay online!" value="Submit"> 
</form> 

También este es el código en ejecución, simplemente me lo hacen más fácil ver cómo funciona, basta con ejecutar el código de abajo para ver el resultado:

function submitForm() { 
 
    return confirm('Do you really want to submit the form?'); 
 
}
<form onsubmit="return submitForm(this);"> 
 
    <input type="text" border="0" name="submit" /> 
 
    <button value="submit">submit</button> 
 
</form>

Cuestiones relacionadas