开发者

function that adds <SPAN>

Good morning, I have a doubt. I have a function that adds a click the User, the side of my input

Plus this gives an error. I wanted just once even if adicionace a person clicks the button several times the action. Do you have a scan after input if not add another

My code.

//th = Name of the input that will add, at the low <SPAN>

      if(d.length =开发者_StackOverflow社区= 0){
          $(th).after('<SPAN class="erro">'+txt+'</SPAN>');
          this.focus();
          return false;
      }

I would be very grateful for the help. :)


You should search for span.error first with $.find, and add it only if it isn't there already:

  if(d.length == 0){
      var errorSpan = $(th).find("span.error").length;
      if(!errorSpan) {
          $(th).after('<SPAN class="erro">'+txt+'</SPAN>');
          this.focus();
      }
      return false;
  }


use jQuery One. Attach a handler to an event for the elements. The handler is executed at most once per element.

$('#yourbuttonid').one('click', function() {
  if(d.length == 0){
      $(th).after('<SPAN class="erro">'+txt+'</SPAN>');
      this.focus();
      return false;
  }
});


Register the event using $('selector').one('click', function(e) { /* your code */ }); to make it fire only once.

If th is the input name (a string), your selector should be like $('input[name='+th+']') instead of $(th)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜