jQuery 克隆函数后更新子元素的属性
我有一些代码似乎可以在旧版本的 jQuery 上正常工作,我认为直到 1.4 为止。我注意到它在 1.4.4 中肯定停止工作。并且仍然不适用于 1.6.2。我做了一些研究,在这个问题上有一些类似的问题,但我还没有找到一个场景,在执行克隆()之后,除了 id 之外还需要更新其他内容。
有谁知道我如何重写它以便它可以与当前版本的 jQuery 一起使用?
//Add dynamic Shot handling
$('#link_addShot').click(function() {
var cloned = $('#tbl_shots tbody tr:last').clone(true);
prevNum = numOfShots;
numOfShots = numOfShots + 1;
$('#shot_number_' + prevNum, cloned).attr('name', 'shot_number_' + numOfShots);
$('#shot_type_id_' + prevNum, cloned).attr('name', 'shot_type_id_' + numOfShots);
$('#player_id_' + prevNum, cloned).attr('name', 'player_id_' + numOfShots);
$('#shot_number_' + prevNum, cloned).attr('id', 'shot_number_' + numOfShots);
$('#shot_type_id_' + prevNum, cloned).attr('id', 'shot_type_id_' + numOfShots);
$('#player_id_' + prevNum, cloned).attr('id', 'player_id_' + numOfShots);
$('#removeLink', cloned).html('<a href="#">Remove</a>');
$('#removeLink a', cloned).click(function() {
$(this).parent().parent().parent().remove();
return false;
})
$('#shot_type_id_' + numOfShots, cloned).val(0);
$('#player_id_' + numOfShots, cloned).val(0);
$('#tbl_shots').append(cloned);
$(this).removeClass('form_button_hover');
return false;
});
//remove link handling
$(".link_remove").click(function() {
$(this).parent().parent().parent().remove();
return false;
});
哦,如果我包含我试图克隆的 tr 标记,它可能会有所帮助:
<tr>
<td><input id="shot_number_5" type="hidden" value="" name="shot_number_5">
<select id="shot_type_id_5" class="shot_type_id" name="shot_type_id_5">
<option value="0">Unknown</option>
<option value="1">Tee</option>
<option value="2">Fairway</option>
<option value="3">Rough</option>
<option value="4">Bunker (fairway)</option>
<option value="8">Bunker (greenside)</option>
<option value="5">Fringe</option>
<option selected="selected" value="6">Green</option>
<option value="7">Penalty</option>
<option value="9">Concede</option>
<option value="10">Pick Up</option>
</select></td>
<td class="td_last" align="center"><div id="removeLink"> <a class="link_remove" href="">Remove</a> </div></td>
</tr>
I had some code that seemed to work ok with older versions of jQuery, up until 1.4 I think. I noticed it stopped working for sure with 1.4.4. And still doesn't work with 1.6.2. I did some research and there are some similar questions on this issue, but I haven't found a scenario where something more than just the id has to be updated after doing a clone().
Does anyone know how I can re-write this so it'll work with the current version of jQuery?
//Add dynamic Shot handling
$('#link_addShot').click(function() {
var cloned = $('#tbl_shots tbody tr:last').clone(true);
prevNum = numOfShots;
numOfShots = numOfShots + 1;
$('#shot_number_' + prevNum, cloned).attr('name', 'shot_number_' + numOfShots);
$('#shot_type_id_' + prevNum, cloned).attr('name', 'shot_type_id_' + numOfShots);
$('#player_id_' + prevNum, cloned).attr('name', 'player_id_' + numOfShots);
$('#shot_number_' + prevNum, cloned).attr('id', 'shot_number_' + numOfShots);
$('#shot_type_id_' + prevNum, cloned).attr('id', 'shot_type_id_' + numOfShots);
$('#player_id_' + prevNum, cloned).attr('id', 'player_id_' + numOfShots);
$('#removeLink', cloned).html('<a href="#">Remove</a>');
$('#removeLink a', cloned).click(function() {
$(this).parent().parent().parent().remove();
return false;
})
$('#shot_type_id_' + numOfShots, cloned).val(0);
$('#player_id_' + numOfShots, cloned).val(0);
$('#tbl_shots').append(cloned);
$(this).removeClass('form_button_hover');
return false;
});
//remove link handling
$(".link_remove").click(function() {
$(this).parent().parent().parent().remove();
return false;
});
Oh, it'll probably help if I include the tr markup I'm trying to clone:
<tr>
<td><input id="shot_number_5" type="hidden" value="" name="shot_number_5">
<select id="shot_type_id_5" class="shot_type_id" name="shot_type_id_5">
<option value="0">Unknown</option>
<option value="1">Tee</option>
<option value="2">Fairway</option>
<option value="3">Rough</option>
<option value="4">Bunker (fairway)</option>
<option value="8">Bunker (greenside)</option>
<option value="5">Fringe</option>
<option selected="selected" value="6">Green</option>
<option value="7">Penalty</option>
<option value="9">Concede</option>
<option value="10">Pick Up</option>
</select></td>
<td class="td_last" align="center"><div id="removeLink"> <a class="link_remove" href="">Remove</a> </div></td>
</tr>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,如果其他人正在寻找类似的东西,找到了解决方案。我认为这既是一个逻辑问题,也是一个语法问题。但不确定我是否真的需要更改语法。逻辑问题是 prevNum 实际上不正确,所以我将其更改为使用 find() 函数,并在 ID 后添加通配符,因为我知道克隆元素中只有其中一个。
Ok, if anyone else is looking for something similar, found a solution. I think it was both a logic issue and a syntax issue. But not sure if I really needed to change the syntax or not. The logic issue was the prevNum was actually incorrect, so instead I changed it to use a find() function with a wildcard after the ID, cause I know there is only one of those within the cloned element.