Where to put $.validator.setDefaults({ onkeyup: fa

2019-07-07 00:53发布

问题:

I have seen many answers for how to disable the onKeyup setting that the MVC3 Remote Validator has. The responses I have seen is to enter this:

$.validator.setDefaults({ onkeyup: false });

However I am not certain where to put this. I have tried putting it in its own script block at the head of my layout view, I have tried putting it in the jquery.validate.js file of my MVC3 project. Neither seems to work. So where do I put this script within my project to make it work?

回答1:

You have to tell the code to execute after the DOM has been parsed. To do this, simply wrap the code in a $() function:

$(function() {
    $.validator.setDefaults({ onkeyup: false });
}

This will ensure that the page elements will load, and then the script will be executed, setting your defaults.