2016-08-12 8 views
0

私はボタンクリックイベントで動作する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 
    } 

}); 

答えて

1

あなたは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ドキュメントの詳細この関数を行うことができます。

関連する問題