Jquery 从同一位置的不同阵列中删除两个项目
我有两个独立的数组,看起来像这样Jquery 从同一位置的不同阵列中删除两个项目,jquery,Jquery,我有两个独立的数组,看起来像这样 12-Jan-12 (remove), 14-Mar-12 (remove) £139, £187 当有人单击数组中的移除按钮时,我需要它从第二个数组中的相同位置移除该按钮 因此,如果我在12-Jan-12上单击删除,那么我希望它从第二个数组中删除£139 下面是一些我已经从第一个数组中删除的代码 $('.delete-date').live('click', function() { $(this).closest('.myDate').remov
12-Jan-12 (remove), 14-Mar-12 (remove)
£139, £187
当有人单击数组中的移除按钮时,我需要它从第二个数组中的相同位置移除该按钮
因此,如果我在12-Jan-12
上单击删除,那么我希望它从第二个数组中删除£139
下面是一些我已经从第一个数组中删除的代码
$('.delete-date').live('click', function() {
$(this).closest('.myDate').remove();
});
这是我的HTML
<span class="dates">
<span class="myDate">
12-jan-12
<span class="delete-date" title="remove this tag" />
</span>,
<span class="myDate">
14-mar-12
<span class="delete-date" title="remove this tag" />
</span>,
</span>
<span class="costs">
<span class="myCost">
£139
</span>,
<span class="myCost">
£187
</span>,
</span>
2012年1月12日
,
2012年3月14日
,
£139
,
£187
,
有什么想法吗
提前感谢您希望合并
.index()
及
例如,在单击事件处理程序中
var dateIndex = $(".dates").index($(this));
prices.eq(dateIndex).hide();
使用jQuery,您可以使用
.index()
方法获取同级元素的索引,并使用.eq()
方法根据其索引选择元素。因此:
var d = $(this).closest('.myDate');
var index = d.index();
d.remove();
$(".costs .myCost").eq(index).remove();
NB:不是使用<代码> .LIVER()/Cude>,您应该考虑使用<代码>委托()>代码>(或<代码> .[())/jcQuery 1.7 ],因为它更有效。
< P>可以使用Index()方法获取元素的索引,并找到第二个集合中的匹配索引。$('.delete-date').live('click', function() {
var pos = $(this).closest('.myDate').index();
$(this).closest('.myDate').remove();
$('.costs .myCost').eq(pos).remove();
});
只是澄清一下,您没有将其存储在实际数组中,对吗?只是HTML表示,对吗?@JohnP是的,没错,我在Asp.Net代码中使用HTML表示,并将其用作数组。谢谢-效果很好,我以前没有遇到过
.index()
,我相信我也没有研究过仍在使用1.6的jQuery 1.7。我将查看delegate()
和on()
-再次感谢
$('.delete-date').live('click', function() {
var pos = $(this).closest('.myDate').index();
$(this).closest('.myDate').remove();
$('.costs .myCost').eq(pos).remove();
});