2011-03-20 54 views
1

早上好,我有疑問。 我有一個功能,添加一個點擊用戶,我的輸入端功能增加<SPAN>

此外,這給了一個錯誤。即使adicionace一個人點擊了按鈕幾次,我也只想要一次。你有沒有輸入後掃描,如果不添加另一個

我的代碼。

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

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

我將非常感謝您的幫助。 :)

回答

0

你應該尋找span.error先用$.find,並添加它,只有當它不存在的話:

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

感謝,所以我想,更多的嘗試並沒有在這裏工作。 var varSpan = $(th)。 find(「span.error).length;和 var errorSpan = $(th).prev(」span.error)。長度; 其中「errorSpan」值= 0 – user628298 2011-03-20 10:58:31

+0

@ user628298:'「span.error」',而不是'「span.error' - 是一個錯字? – Jon 2011-03-20 11:03:02

+0

現在感謝它的工作,我把父母 $(th) 。家長( 「diinput 」)找到(「 span.erro」)長度。; – user628298 2011-03-20 11:10:38

0

使用jQuery One。將處理程序附加到元素的事件。處理程序每​​個元素最多執行一次。

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

註冊使用$('selector').one('click', function(e) { /* your code */ });,使之火一次事件。

如果th是輸入名稱(字符串),你的選擇應該像$('input[name='+th+']'),而不是$(th)