我应该将代码添加到以下代码中:
这是html:
<div class="wrap">
<div class="item">
<a href="img/american-airlines.jpg">
American Airlines
</a>
</div>
<div class="item">
<a href="img/united-airlines.jpg">
United Airlines
</a>
</div>
<div class="item">
<a href="img/united-airlines.jpg">
United Airlines
</a>
</div>
</div>
这是我的剧本:
$(".wrap").find(".item a").each(function () {
var xx = ( $(this).attr('href'));
$("body").append('<label><input type="checkbox" value="' + xx +'">' + xx + '</label>');;
});
答案 0 :(得分:1)
这是一支固定笔:http://codepen.io/omerts/pen/YWzgjQ?editors=1010
$(".wrap").find(".item a").each(function () {
var xx = ( $(this).attr('href'));
if (!$('input[value="'+ xx +'"]').length) {
$("body").append('<label><input type="checkbox" value="' + xx +'">' + xx + ' (<span>1</span>) </ label>');
} else {
var currentCount = $('input[value="'+ xx +'"]').next('span')
var newCount = parseInt(currentCount.text()) + 1
currentCount.text(newCount)
}
});