-1
<fieldset id="tt">
    <div class="cts">
        <div class="tbox">
            <input type="text" class="pp"/>
        </div>
    </div>
    <div class="cts">
        <div class="tarea">
            <input type="text">
        </div>
    </div>
    <div class="cts">
        <div class="tbox">
            <input type="text" class="pp"/>
        </div>
    </div>
    <div class="cts">
        <div class="tarea">
            <input type="text">
        </div>
    </div>
</fieldset> 
$('.cts').find('div:first').each(function(i){
    var attr = $(this).attr('class');
    alert(attr)
    switch(attr) {
        case "tbox":
            $('<div class="fieldProperties">        <input type="text" value="first" name="fields['+i+'].id" id="fields['+i+'].id"> </div>')
                .insertAfter($('.cts').find('div.tbox'));
            break;

        case "tarea":
            $('<div class="fieldProperties">        <input type="text" value="second" name="fields['+i+'].id" id="fields['+i+'].id"></div>')
                .insertAfter($('.cts').find('div.tarea'));
            break;
    }
});

在这里,我试图在插入最新的<div class="properties">之后插入<div class="tbox">,但在这里插入每个 div。每次都会加载一些字段属性<div class="tarea"><div class="properties">

我正在低于 DOM 结构,但我不想这样

<fieldset id="tt">
    <div class="cts">
        <div class="tbox">
            <input type="text" class="pp">
        </div><div class="fieldProperties"><input type="text" id="fields[2].id" name="fields[2].id" value="first"></div><div class="fieldProperties"><input type="text" id="fields[0].id" name="fields[0].id" value="first"></div>
    </div>
    <div class="cts">
        <div class="tarea">
            <input type="hidden">
        </div><div class="fieldProperties"><input type="text" id="fields[3].id" name="fields[3].id" value="first"></div><div class="fieldProperties"><input type="text" id="fields[1].id" name="fields[1].id" value="first"></div>
    </div>
    <div class="cts">
        <div class="tbox">
            <input type="text" class="pp">
        </div><div class="fieldProperties"><input type="text" id="fields[2].id" name="fields[2].id" value="first"></div><div class="fieldProperties"><input type="text" id="fields[0].id" name="fields[0].id" value="first"></div>
    </div>
    <div class="cts">
        <div class="tarea">
            <input type="hidden">
        </div><div class="fieldProperties"><input type="text" id="fields[3].id" name="fields[3].id" value="first"></div><div class="fieldProperties"><input type="text" id="fields[1].id" name="fields[1].id" value="first"></div>
    </div>
</fieldset>

我正在尝试使用dom结构

<fieldset id="tt">
    <div class="cts">
        <div class="tbox">
            <input type="text" class="pp">
        </div><div class="fieldProperties"><input type="text" id="fields[0].id" name="fields[0].id" value="first"></div>
    </div>
    <div class="cts">
        <div class="tarea">
            <input type="hidden">
        </div><div class="fieldProperties"><input type="text" id="fields[1].id" name="fields[1].id" value="first"></div>
    </div>
    <div class="cts">
        <div class="tbox">
            <input type="text" class="pp">
        </div><div class="fieldProperties"><input type="text" id="fields[2].id" name="fields[2].id" value="first"></div>
    </div>
    <div class="cts">
        <div class="tarea">
            <input type="hidden">
        </div><div class="fieldProperties"><input type="text" id="fields[3].id" name="fields[3].id" value="first"></div>
    </div>
</fieldset>
4

1 回答 1

0

在评论中澄清自己之后,我认为这就是您要寻找的:

jsFiddle 演示

$('.cts').find('div.tbox, div.tarea').each(function(i) {

    var val;

    switch ($(this).attr("class"))
    {
         case ("tbox"):
             val = "first";
             break;
         case ("tarea"):
             val = "second";
             break;
    }

    var properties = 
      $('<div class="fieldProperties">' +
        '<input type="text" value="' + val + '" name="fields['+i+'].id" ' +
        'id="fields['+i+'].id"> </div>');    

    properties.insertAfter(this);
});
于 2013-08-28T13:44:13.233 回答