2012-03-14 44 views
0

即時工作在jQuery中的表單字段突出顯示,目前它的工作原理除了它只適用於窗體的第一行,其餘的字段不註冊我的焦點事件,這樣我可以將焦點放到我目前使用下面的jQuery代碼jquery類選擇器只註冊類的第一個元素上的事件

var debug = false; 




    $(function(){ 

     $(".email-form tbody:last").AddFormLineToTable(); 

     $("#do-add-row").click(function() { 

      if(debug) 
      { 
       alert("serialize click eventhandler fired"); 
      } 

      $(".email-form tbody:last").AddFormLineToTable(); 
     }); 



     $(".does-focus-highlight").focusin(function(e){ 

      $(e.target).addClass("focus-highlight"); 
      if(debug){alert('field did focus');} 
     }); 
     $(".does-focus-highlight").focusout(function(e){ 
      $(e.target).removeClass("focus-highlight"); 
      if(debug){alert('field did blur');} 

     }); 


     $("#do-serialize").click(function(){ 
      if(debug) 
      { 
       alert("serialize click eventhandler fired"); 
      } 
      var jsn = $("#contact-form").serializeArray(); 
     $("#serialize-target").val(JSON.stringify(jsn,null,2)); 



     }); 



    }); 

格式化 是有什麼我在想念趕上這些附加事件 未燒製被dunamically產生,以及如果表單字段這也產生 哪些產生如下

var lineCount = 0; 

jQuery.fn.AddFormLineToTable = function() { 
    var o = $(this[0]); 
    o.append('<tr id="contact-'+lineCount+'">' + 
     '<td>Name: <input class="does-focus-highlight" id="contact-name-'+lineCount+'" name="contact-name-'+lineCount+'" type="text" required="required" /></td>' + 
     '<td>Email: <input class="does-focus-highlight" id="contact-email-'+lineCount+'" name="contact-email-'+lineCount+'" type="email" required="required" /></td>' + 
     '</tr>'); 

    lineCount++; 
}; 
+0

你能爲此提供jsfiddle.net嗎? – sandeep 2012-03-14 11:16:25

+0

爲什麼選擇一個空的Objext('$(this [0])'),然後應用一個jQuery方法('append')? – m90 2012-03-14 11:17:26

+0

$(this [0])獲取選擇器中的第一個對象 – 2012-03-14 21:22:51

回答

0

.click()僅將事件綁定到調用.click()時存在的元素。如果您想要爲稍後創建的元素處理事件,請使用.on().live(),具體取決於您正在使用的jQuery版本。閱讀非常有用的信息here

相關問題