克隆包含选择的行,然后克隆选择'价值观
我有这样的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以手动执行此操作:
http://jsfiddle.net/Tp7hg/
You can do it manually:
http://jsfiddle.net/Tp7hg/
使用选择元素
selectedIndex
尝试此操作Try this with using the select elements
selectedIndex
您可以将一个函数传递给
val
来提供索引。另外,我建议将 jQuery 对象存储在变量中,这样您就不必为每个选择构建它。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.