为什么这个表格总是提交。。。验证javascript不起作用
此表单始终提交,从不因错误而停止。怎么了 提前感谢;) 我把提交的返回valida(这个)。。。。而且javascript代码看起来很好为什么这个表格总是提交。。。验证javascript不起作用,javascript,forms,validation,Javascript,Forms,Validation,此表单始终提交,从不因错误而停止。怎么了 提前感谢;) 我把提交的返回valida(这个)。。。。而且javascript代码看起来很好 <form name="formulario" id="formulario" method="POST" action="<%=request.getContextPath()%>/altaanuncio" onSubmit="return valida(this)"> function valida(f) {
<form name="formulario" id="formulario" method="POST" action="<%=request.getContextPath()%>/altaanuncio" onSubmit="return valida(this)">
function valida(f) {
if (f.marca.selectedIndex==0){
$('#errores').html('Seleccione la marca.');
f.marca.focus();
return false;
}
else if(f.garantia.value == ""){
$('#errores').html('Introduzca meses de garantía');
f.garantia.focus();
return false;
}
else if(f.pvpofertado.value == ""){
$('#errores').html('Introduzca el precio del coche');
f.pvpofertado.focus();
return false;
}
return true;
}
函数valida(f){
如果(f.marca.selectedIndex==0){
$(“#errors”).html('Seleccione la marca');
f、 marca.focus();
返回false;
}
else if(f.garantia.value==“”){
$(“#errors').html('introzca meses de garantia');
f、 garantia.focus();
返回false;
}
else if(f.pvpofertado.value==“”){
$(“#errors').html('introzzca el precio del coche');
f、 pvpofertado.focus();
返回false;
}
返回true;
}
您显然使用jQuery,所以请始终使用jQuery
在这里可以找到验证插件:
并在此处发布:
$(文档).ready(函数(){
$(“#formulario”).bind(“submit”,函数(e){
var f=this;//或$(this),但是您需要测试.val()并使用find()或children
如果(f.marca.selectedIndex==0){
$(“#errors”).html('Seleccione la marca');
f、 marca.focus();
e、 预防默认值();
}
.
.
.
.
}
$.post($(this.attr('action'),$(this.serialize(),
成功:功能(响应){
控制台日志(响应);
}
});
e、 preventDefault();//取消实际提交
});
});
我认为您试图调用普通JS元素上的jQuery函数。试试这个:
// When the DOM is ready:
$(document).ready(function() {
// Attach submit event listener to form (== onSubmit)
$('#formulario').submit(function(event) {
// Get the jQuery object for the 'marca' field:
var marca = event.target.children('#marca');
if (marca.attr('selectedIndex') === 0) {
$('#errores').text('Seleccione la marca.');
marca.focus(); // <--this now works because it's a jQuery object.
event.preventDefault(); // instead of return false;
}
});
});
<form id="formulario" />
//DOM就绪时:
$(文档).ready(函数(){
//将提交事件侦听器附加到表单(=onSubmit)
$(“#formulario”).submit(函数(事件){
//获取“marca”字段的jQuery对象:
var marca=event.target.children(“#marca”);
if(marca.attr('selectedIndex')==0){
$(“#errors”).text('Seleccione la marca');
marca.focus();//您是否尝试放置valida(this)而不是返回valida(f)?确定您使用的是什么服务器端语言/框架?@lc2817在纯JS中,它当然必须是返回valida(this)
Ah,@mplungjan打败了我。我知道会发生什么。我有AJAX SUBMIT!!!!然后它总是提交,是否有任何调用函数并停止提交的模式?但是我今天的连接非常慢,所以我永远都无法修复拼写错误;)应该停止提交事件执行其常规操作(即通过常规方式提交表单)。如果您仍在使用AJAX进行提交,则这可能位于提交()的顶部事件处理程序。请参阅@mplungjan的答案。@PPvG在每次聚焦后不继续验证的最优雅方式是什么?返回false?我知道会发生什么。我有AJAX提交!!!!然后它总是提交,是否有任何调用函数和停止提交的模式?当然。请参阅更新。我将添加一个示例,但我的互联网很慢现在不行
// When the DOM is ready:
$(document).ready(function() {
// Attach submit event listener to form (== onSubmit)
$('#formulario').submit(function(event) {
// Get the jQuery object for the 'marca' field:
var marca = event.target.children('#marca');
if (marca.attr('selectedIndex') === 0) {
$('#errores').text('Seleccione la marca.');
marca.focus(); // <--this now works because it's a jQuery object.
event.preventDefault(); // instead of return false;
}
});
});
<form id="formulario" />