开发者

calling the jq function like you would do a js function from a form element

i am trying to validate a form using jqu开发者_Python百科ery (not use the validate plugin)...what i wanted to know was is there a way to call the same function without it being attached to a particular node...

like in js

<input id="xyz" ....onblur=js:function()>

<input id="abc" ....onblur=js:function()>

where as in jq

$(#xyz).blur(); 
$(#abc).blur(); 

not the exact syntax but you do get it...

thanks andy


$(function() {
  $(":input").blur(function() { 
    // executes this function every time the blur event
    // fires on any input element
  });
}

"input element" being input, textarea, select and button elements.

If you just want it to be executed when the blur event fires for regular inputs, lose the colon. Hope I got what you wanted to achieve.


I hope I'm understanding this right. To attach those event handlers unobtrusively, use a document.ready handler, and add them inside, like this:

$(function() { //runs when the DOM is ready
  $("#xyz").blur(function() {
    //do some validation...
  }); 
  $("#abc").blur(function() {
    //do some validation...
  }); 
});

Then your handlers need no inline functions, they can just be:

<input id="xyz" name="xyz" type="text">
<input id="abc" name="abc" type="text">


$('#xyz, #abc').blur(function() {
    //Wrap into jQuery object
    var $this = $(this);

    //console.log($this);

    //Do some validations
    $this.val($.trim($this.val()));
    if(!$this.val().length) {
        alert('box is empty');
    }
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜