使用jQuery将select标记附加到表单中

时间:2014-07-02 20:09:33

标签: javascript jquery web domdocument

我需要在jquery中将select标签附加到表单中 当我尝试追加jquery时,将一个对象字符串附加到html中 有人可以指出我出错的地方

HTML

 <table>
    <tr>
        <th>Test</th>
        <th>Entities</th>
        <th>Group</th>
        <th>Members</th>
    </tr>

    <tr class=color_one>
        <td> Flow </td>
        <td>    

                mT1127-1 <br> 

                mT1127-2 <br> 

                mT1127-3 <br> 

        </td>
        <td>
            <select class="group_select_Flow" id="group" name="group"><option value=""></option>
<option value="369 - Antibody Validations Flow">369 - Antibody Validations Flow</option>
<option value="396 - Flow">396 - Flow</option>
<option value="389 - Flow">389 - Flow</option></select>
        </td>
        <td>
            <select class="group_member_select_Flow" id="group_members" name="group_members"></select> &nbsp
            <a href="#" style = "text-decoration:none;color:darkblue;" class="add_more">+</a>
        </td>
    </tr>

    <tr class=color_two>
        <td> Western </td>
        <td>    

                mT1127-1 <br> 

                mT1127-2 <br> 

                mT1127-3 <br> 

        </td>
        <td>
            <select class="group_select_Western" id="group" name="group"><option value=""></option>
<option value="369 - Antibody Research Western">369 - Antibody Research Western</option>
<option value="369 - Antibody Validations Western">369 - Antibody Validations Western</option>
<option value="378 - Antibody Support Western">378 - Antibody Support Western</option>
<option value="389 - Western">389 - Western</option>
<option value="392 - Western">392 - Western</option>
<option value="396 - Western">396 - Western</option>
<option value="Antibody Western">Antibody Western</option></select>
        </td>
        <td>
            <select class="group_member_select_Western" id="group_members" name="group_members"></select> &nbsp
            <a href="#" style = "text-decoration:none;color:darkblue;" class="add_more">+</a>
        </td>
    </tr>

</table>

SCRIPT

$(document).ready(function(){
$("a.add_more").on("click", function(){
    var closest_td = $(this).closest("td");
    var select_tag = closest_td.find("select");
    closest_td.append("<br>" + $(select_tag));
});//add_more.on click
});//dom ready

http://jsfiddle.net/3vTAH/

1 个答案:

答案 0 :(得分:4)

您正在将jQuery DOM元素集合转换为字符串。您需要附加新的DOM元素或现有的元素。

$(document).ready(function(){
    $("a.add_more").on("click", function(){
        var closest_td = $(this).closest("td");
        var select_tag = closest_td.find("select:first");
        closest_td.append("<br>").append(select_tag.clone());
    });//add_more.on click
});//dom ready

http://jsfiddle.net/3vTAH/2/