2012-03-14 86 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

相关问题