0

いくつかのID名を持つ選択ドロップダウンがあります。javascriptを使用して動的に別のID名でクローンを作成したい。これが私のコードです。

<td>
            <div style="float: left;">
                <g:select name="degree.id" from="${DomainName.list()}" optionKey="id"
                          optionValue="title" noSelection="['': '']" id="degree"
                          value="${cvEducationDetailCO?.degree?.id}" onchange="changeGradeSelectData(this.value)"/>
            </div>

        </td>
<a href="javascript:void (0);" onclick="addAnotherSelectBox();">Add Another Select Box </a>

このためのjqueryは何ですか?

tr と td があるテーブルがあります。その td には、選択ボックスがあります。選択ボックスのクローンを作成し、それを新しい行に配置する必要があります。ユーザーは、必要に応じて多くの選択ボックスを生成できます。そのため、動的trを生成し、新しいIDで選択ボックスのクローンを作成したいと考えています。

4

1 に答える 1

2

jQuery を使用している場合は、次のように言えます。

var newSelect = $("#degree").clone().prop("id", "newIdHere");

そして、次の方法でページに追加できます。

newSelect.appendTo("selector of parent element here");
// OR
$("selector of parent").append(newSelect);
// e.g., to add to same div as original:
$("#degree").parent().append(newSelect);

onclick=...特に jQuery を既に使用している場合は、 のようなインライン イベント属性を含めない方がよいでしょう。

<a href="#" id="addSelect">Add Another Select Box </a>

その後:

$(document).ready(function() {
    $("#addSelect").click(function(e) {
        e.preventDefault();
        var newSelect = $("#degree").clone().prop("id", "newIdHere");
        newSelect.wrap("<tr><td></td></tr>").closest("tr").appendTo("#idOfTableHere");
    });
});
于 2013-06-16T06:04:26.220 に答える