2010-03-24 190 views
-1

此代码产生一团糟...我做错了什么?Jquery表格单元格

cell=$("<td>"); 
     if(normal.exam_type=="Exam_Boolean") 
     { 
      var input=cell.append("<input>").last(); 

      input.attr("type","hidden"); 
      input.attr("name","exam.exam_Normal['" +normal_id_unique + "'].boolean_v"); 
      input.attr("value",normal.normal_boolean);    

回答

1

我会做这种方式:

var cell = $("<td></td>"); 
if(normal.exam_type=="Exam_Boolean") 
{ 
    $("<input />").attr("type", "hidden") 
      .attr("name", "exam.exam_Normal['" +normal_id_unique + "'].boolean_v") 
      .attr("value", normal.normal_boolean) 
      .appendTo(cell); 
); 

[点之前的换行符只是为了方便阅读。]

在追加东西之前,您可能还需要将单元放置在某个文档中。我不确定

+0

它似乎工作... 但appendTo似乎并没有返回一个引用$(“input />”) – GorillaApe 2010-03-24 08:25:04

+0

它完全应该。那么它返回什么?登录到萤火虫,并看看 – naugtur 2010-03-24 12:47:23

0

而不是使用cell.append(input) - 它返回一个参考cell - 尝试做 “周围的其他方式” 为input.appendTo(cell)

var cell = $("<td>"); 
if(normal.exam_type=="Exam_Boolean") 
{ 
    var input= $("<input />").appendTo(cell); 
    input.attr("type", "hidden"); 
    input.attr("name", "exam.exam_Normal['" + normal_id_unique + "'].boolean_v"); 
    input.attr("value", normal.normal_boolean); 
); 
+0

好主意,我会尽量 – GorillaApe 2010-03-24 08:07:43

+0

给了我一个jQuery例外:( – GorillaApe 2010-03-24 08:12:48

+0

@Parhs:为什么,它看起来不错我 – 2010-03-24 08:36:48

0

它的种类取决于你想要的代码。如果要附加在细胞中的最后一个项目之后的输入,然后尝试:

cell=$("<td>"); 
if(normal.exam_type=="Exam_Boolean") 
{ 
    var input=cell.last().after("<input>"); 

    input.attr("type","hidden"); 
    input.attr("name","exam.exam_Normal['" +normal_id_unique + "'].boolean_v"); 
    input.attr("value",normal.normal_boolean); 
}; 

如果不这样做,你的愿望是什么,也许你应该张贴一些HTML代码和你想要什么更好的说明你的代码要做。

+0

谢谢你好吧 – GorillaApe 2010-03-24 09:37:57

+0

不客气! – lugte098 2010-03-24 10:05:37