如何向另一个脚本中设置的jQuery验证submitHandler添加其他功能?我无法首先修改定义submitHandler的源JavaScript。相反,我需要从后续运行的脚本中添加一些额外的功能。下面是.js文件的一些缩小的伪代码,它首先定义了submitHandler - 我无法直接修改它:
$("#frmReg").validate({
invalidHandler: function(e, validator) { //code here ... not relevant },
submitHandler: function() {
window.onbeforeunload = null;
$('#myForm')[0].submit();
},
rules: { firstname: { required: true } },
messages: { firstname: { required: 'First name is required' } }
});
基本上,我想将第二个函数绑定到现有的submitHander。
答案 0 :(得分:0)
我浏览了消息来源,我认为这应该有效:
var validatorInstance = $("#frmReg").validate(), //Call this AFTER the initial registration,
submitHandler = validatorInstance.settings.submitHandler;
validatorInstance.settings.submitHandler = function(){
//Do your custom stuff here
console.log(true);
return submitHandler.apply(this,arguments); //forward to the old handler
};