如果任何字段错误,LiveValidation会阻止触发表单操作,但不会停止ajax调用。
所以我想做点什么:
if (field1.passValidation) {
ajax_call();
}
这是代码(用西班牙语):
$('.reporta').click(function() {//.reporta is the submit field of the form
var motivo = $('#reportar select').val();
var razon = $('#reportar textarea').val();
reportar(v_id, motivo, razon); //Ajax call
});
//creation of LiveValidation object
var reporte_f1 = new LiveValidation('reporte_f1', {validMessage: " ", wait: 300});
reporte_f1.add(Validate.Presence, {failureMessage: "Por favor, escriba un motivo"});
<form action="#">
<table>
<tr><td><label for="reporte_f1">Motivo del reporte:<br /></label><textarea id="reporte_f1" rows="7" cols="50"></textarea></td></tr>
</table>
<div>
<input type="submit" class="reporta" value="Reportar" />
</div>
</form>
这样做,如果该字段未通过LiveValidation,则无论如何都会发送表单。
答案 0 :(得分:0)
<强> jsFiddle Demo 强>
<强> HTML 强>
<form action="#">
<table>
<tr>
<td>
<label for="reporte_f1">Motivo del reporte:
<br />
</label>
<textarea id="reporte_f1" rows="7" cols="50"></textarea>
</td>
</tr>
</table>
<div>
<input type="submit" class="reporta" value="Reportar" />
</div>
</form>
<强> JS 强>
function reportar(v_id, motivo, razon) {
$('.reporta').html('<img src=\"img/cargando2.gif\" width=\"30px\" height=\"30px\"/>');
$.ajax({
url: "forms.php?form=10",
data: {
v_id: v_id,
motivo: motivo,
razon: razon
},
type: "POST",
success: function (response) {
$(".reportar[rel]").overlay().close();
$('#dialogo').fadeIn(250);
$('#dialogo').html("<ul><li class='ack'>El reporte ha sido enviado correctamente</li></ul>");
setInterval(function () {
$('#dialogo').fadeOut(400);
}, 3000);
},
error: function (xhr, ajaxOptions, thrownError) {
alert("Ha ocurrido un error al cargar los datos");
}
});
}
$(function () {
$('.reporta').click(function () { //.reporta is the submit field of the form
var motivo = $('#reportar select').val();
var razon = $('#reportar textarea').val();
reportar(v_id, motivo, razon); //Ajax call
});
//creation of LiveValidation object
var reporte_f1 = new LiveValidation('reporte_f1', {
validMessage: "Gracias!",
wait: 300
});
reporte_f1.add(Validate.Presence, {
failureMessage: "Por favor, introduzca algo"
});
//The regex pattern here has to be matched
reporte_f1.add(Validate.Format,
{ pattern: /^\w+$/i, failureMessage: "Por favor, introduzca un mensaje válido" } );
});
注意强>
我通过查看以下example找到了这一点。
也取自以下example
当LiveValidation对象被实例化时,如果它发现它是表单的子对象,它会自动将自身附加到表单的提交事件,因此在提交时,对其中的所有LiveValidation对象进行验证将是自动执行。如果任何验证失败,它将停止表单提交(这当然只有在用户bowser中启用javascript时才有效,所以一定要在服务器上进行一些验证!)。 您还可以在创建LiveValidation对象时将onlyOnSubmit选项指定为true,以使其仅在提交表单时进行验证。这在以下示例中进行了演示。 另请注意,电子邮件字段没有验证它是否存在,因此如果未填写表单中的内容仍然有效,因为它将被视为可选字段。 在此示例中,如果结果有效,则结果将被覆盖,因为此页面无处可提交 - 而不是提交,它将提醒它有效,在现实生活中它将继续提交,如果有效。