用jQuery创建许多不同的元素 - 追加

时间:2013-01-03 08:57:43

标签: jquery append

    var i1 = $("<input type='radio' class='11' value='11' /><input type='radio' class='2' value='2' />");
    var i2 = $("<input type='radio' class='111' value='111' /><input type='radio' class='12'  value='12' /><input type='radio' class='21' value='21' /><input type='radio' class='3' value='3' />");
    var i3 = $("<input type='radio' class='1111' value='1111' /><input type='radio' class='121'  value='121' /><input type='radio' class='13' value='13' /><input type='radio' class='31' value='31' /><input type='radio' class='22' value='22' /><input type='radio' class='4' value='4' />");
    var i4 = $("<input type='radio' class='11111' value='11111' /><input type='radio' class='122'  value='122' /><input type='radio' class='212' value='212' /><input type='radio' class='23' value='23' /><input type='radio' class='32' value='32' /><input type='radio' class='14' value='14' />");
    var i5 = $("<input type='radio' class='111111' value='111111' /><input type='radio' class='123'  value='123' /><input type='radio' class='132' value='132' /><input type='radio' class='213' value='213' /><input type='radio' class='222' value='222' /><input type='radio' class='33' value='33' /><input type='radio' class='24' value='24' />");
    b.append(i1);

我怎样才能使这个追加增量i1 i2 i3 i4 ...

我想点击第一个“+1”来显示两个选项是i1.Second点击“+1”显示i2。和i3 i4 i5 i6 ...

然后点击“-1”这个i1.remove()i2 i3删除...

并在“+1”之后上传/插入开关问题..

抱歉,我这么糟糕

working fiddle

1 个答案:

答案 0 :(得分:4)

将对象放在某个数组中并使用each循环迭代它们。

 var ix = [];
   ix.push( $("<input type='radio' class='11' value='11' /><input type='radio' class='2' value='2' />"));
   ix.push( $("<input type='radio' class='111' value='111' /><input type='radio' class='12'  value='12' /><input type='radio' class='21' value='21' /><input type='radio' class='3' value='3' />"));
   ix.push( $("<input type='radio' class='1111' value='1111' /><input type='radio' class='121'  value='121' /><input type='radio' class='13' value='13' /><input type='radio' class='31' value='31' /><input type='radio' class='22' value='22' /><input type='radio' class='4' value='4' />"));

$.each(ix, function(index, obj) { 
  b.append(obj);
});