克隆包含选择的行,然后克隆选择'价值观

发布于 2024-12-05 09:47:40 字数 1345 浏览 1 评论 0原文

我有这样的 HTML:

<tr>
    <td>
        <input type="hidden" name="MatchId[]" value="">
        <select name="TeamId[]">
            <optgroup label="Women">
                <option value="18">Women 1</option>
                <option value="17">Women 2</option>
            </optgroup>
            <optgroup label="Men">
                <option value="9">Men 1</option>
                <option value="8">Men 2</option>
            </optgroup>
        </select>
    </td>
    <td>
        <select name="Day[]">
            <!-- blah -->
        </select>
    </td>
    <td>
        <input class="addButton" type="button" value="+">
    </td>
    <td>
        <input class="removeButton" type="button" value="-">
    </td>
</tr>

我想在单击 + 按钮时克隆该行,但还将

现在,我有这段代码,它成功克隆了该行,但留下了新的

$('.addButton').livequery('click', function()
{
    var $btn = $(this);

    var $clonedRow = $btn.closest('tr').clone();

    $btn.closest('tbody').append( $clonedRow );
});

我该怎么做?

编辑:附属问题:单击 + 按钮后如何将焦点设置在克隆行的第一个字段上?

I have this HTML:

<tr>
    <td>
        <input type="hidden" name="MatchId[]" value="">
        <select name="TeamId[]">
            <optgroup label="Women">
                <option value="18">Women 1</option>
                <option value="17">Women 2</option>
            </optgroup>
            <optgroup label="Men">
                <option value="9">Men 1</option>
                <option value="8">Men 2</option>
            </optgroup>
        </select>
    </td>
    <td>
        <select name="Day[]">
            <!-- blah -->
        </select>
    </td>
    <td>
        <input class="addButton" type="button" value="+">
    </td>
    <td>
        <input class="removeButton" type="button" value="-">
    </td>
</tr>

I would like to clone the row when I click on the + button, but also set the value of the <select> to be the same as the original row.

For now, I have this code, which successfully clones the row, but leaves the new <select> fields with the first value as a selection:

$('.addButton').livequery('click', function()
{
    var $btn = $(this);

    var $clonedRow = $btn.closest('tr').clone();

    $btn.closest('tbody').append( $clonedRow );
});

How could I do that?

Edit: subsidiary question: how could I set the focus on the first field of the cloned row after I click on the + button?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你在看孤独的风景 2024-12-12 09:47:40

您可以手动执行此操作:

http://jsfiddle.net/Tp7hg/

$('.addButton').live('click', function()
{
    var $btn = $(this);
    var $row = $btn.closest('tr')

    var $clonedRow = $row.clone();
    $clonedRow.find("select").each(function(i){
        this.selectedIndex = $row.find("select")[i].selectedIndex;
    })

    $btn.closest('tbody').append( $clonedRow );
});

You can do it manually:

http://jsfiddle.net/Tp7hg/

$('.addButton').live('click', function()
{
    var $btn = $(this);
    var $row = $btn.closest('tr')

    var $clonedRow = $row.clone();
    $clonedRow.find("select").each(function(i){
        this.selectedIndex = $row.find("select")[i].selectedIndex;
    })

    $btn.closest('tbody').append( $clonedRow );
});
oО清风挽发oО 2024-12-12 09:47:40

使用选择元素 selectedIndex 尝试此操作

$('.addButton').live('click', function()
{
    var $btn = $(this);

    var $clonedRow = $btn.closest('tr').clone();

    var index = $('select', $btn.closest('tr')).prop('selectedIndex');

    $('select', $clonedRow).prop('selectedIndex', index);

    $btn.closest('tbody').append( $clonedRow );
});

Try this with using the select elements selectedIndex

$('.addButton').live('click', function()
{
    var $btn = $(this);

    var $clonedRow = $btn.closest('tr').clone();

    var index = $('select', $btn.closest('tr')).prop('selectedIndex');

    $('select', $clonedRow).prop('selectedIndex', index);

    $btn.closest('tbody').append( $clonedRow );
});
空城之時有危險 2024-12-12 09:47:40

您可以将一个函数传递给 val 来提供索引。另外,我建议将 jQuery 对象存储在变量中,这样您就不必为每个选择构建它。

$('.addButton').live('click', function() {
    var $btn = $(this),
        $row = $btn.closest('tr'),
        $clonedRow = $row.clone();
        $selects = $row.find('select');

    $btn.closest('tbody').append($clonedRow);
    $clonedRow.find('select').val(function(index) {
        return $selects.eq(index).val();
    });
});

You can pass a function into val which gives the index. Also, I'd recommend storing the jQuery objects in a variable so you don't have to build it for each select.

$('.addButton').live('click', function() {
    var $btn = $(this),
        $row = $btn.closest('tr'),
        $clonedRow = $row.clone();
        $selects = $row.find('select');

    $btn.closest('tbody').append($clonedRow);
    $clonedRow.find('select').val(function(index) {
        return $selects.eq(index).val();
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文