我在javascript中有一个函数,可以处理按钮点击事件。我需要这个函数只有在总的表单验证返回true时才能工作。我正在使用jquery验证插件来验证表单。验证工作正常,但当我单击按钮时,即使验证在那里,它仍然在函数内部。
我像这样调用函数:
document.getElementById('btnNext').addEventListener('click', handleFileSelect, false);
function handleFileSelect(evt) {
//My Code goes here
}
//This is my form..
<form id="someID">
<button id ="next" type="submit">
</form>
//And my validation will be as follows:
$("#posterForm").validate({
rules: {
//Here all the validation rules
},
messages: {
//Here all the error messages
}
});发布于 2016-08-12 12:49:16
您混合了dom和jquery事件侦听器。行document.getElementById('btnNext').addEventListener('click', handleFileSelect, false);正在侦听dom事件。你可以直接做
$('#btnNext').on('click', function(evt){
//Validate the fields
var valid;
//set the valid field to true if the fields are true else set to false
if (!valid)
evt.preventDefault();
});preventDefault文档详细介绍了此功能。
https://stackoverflow.com/questions/38910046
复制相似问题