最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jquery live() onkeyup and click - Stack Overflow

programmeradmin1浏览0评论

I'm using the live() function to validate a form that is loaded using ajax, since it wont work without it.

Previously, my form validation was done onkeyup (although I never specified it anywhere). Now it works only when the user clicks the submit button. Is it possible to get my onkeyup validation back as well as retain the verify on submit when using the live() function?

$(".createBtn").live('click', function() {
$("#createAcc").validate({

I'm using the following plugin to do my verification.

bassistance.de/jquery-plugins/jquery-plugin-validation/

I'm using the live() function to validate a form that is loaded using ajax, since it wont work without it.

Previously, my form validation was done onkeyup (although I never specified it anywhere). Now it works only when the user clicks the submit button. Is it possible to get my onkeyup validation back as well as retain the verify on submit when using the live() function?

$(".createBtn").live('click', function() {
$("#createAcc").validate({

I'm using the following plugin to do my verification.

bassistance.de/jquery-plugins/jquery-plugin-validation/
Share Improve this question asked Sep 21, 2012 at 14:53 NormanNorman 6,37526 gold badges91 silver badges148 bronze badges 3
  • 5 As a side note, I would remend using the on() function, since live() is considered deprecated. api.jquery./on – Steven Hunt Commented Sep 21, 2012 at 14:56
  • 1 You can change the 'click' event to anything you want. You probably want to check submit with the submit event. As for a keyup event your question is a bit vague but I'm assuming you mean it's checking an individual field. In that case you need a separate event. keyup may work but personally I'd use blur – Cfreak Commented Sep 21, 2012 at 14:57
  • jQuery validation plugin has built in params for validation on events like onKeyUp, onSubmit etc. By default they are all true unless you explicitly override them. Fyi, live is deprecated in all jQuery versions since 1.7 so upgrade the validation plugin and jQuery accordingly. – Vishal Commented Sep 21, 2012 at 15:07
Add a ment  | 

4 Answers 4

Reset to default 2

You can chain, as Steven said, or you can specify multiple events like this:

http://jsfiddle/gLWRs/1/

$('.createBtn').on({
  click: myFn,
  keyup: myFn    
});​

This also works with live.

Re-reading your question though, you'll probably need something like this, because the events will occur on different elements:

$('.createBtn').on({ click: myFn });
$('input').on({ keyup: myFn });

You can bind multiple events to the same element.. try this

$(".createBtn").live('click keyup', function() {

    yourFunctionCall();
});

Also .live() is deprecated as of the latest jQuery version..

I remend using .on() instead..

You could take your function and set it to a variable as follows:

var myFn = function() { ... };

Then you could register that function with both event handlers.

$(".createBtn").on('click', myFn).on('keyup', myFn);

You can add init of validation into html source that is got from server on document.ready Some like:

if(isAjax)
{
    $(function() {
       // Init you validation
    });
}
发布评论

评论列表(0)

  1. 暂无评论