如何通过增加 id 向每个 div 添加表单复选框?
目前有:
<div class="img-wrap">
<img title="1" src="img.jpg" />
</div>
<div class="img-wrap">
<img title="2" src="img.jpg" />
</div>
<div class="img-wrap">
<img title="3" src="img.jpg" />
</div>
我要
<div class="img-wrap">
<img title="1" src="img.jpg" />
<input type="checkbox" name="1" value="1" />
</div>
<div class="img-wrap">
<img title="2" src="img.jpg" />
<input type="checkbox" name="2" value="2" />
</div>
<div class="img-wrap">
<img title="3" src="img.jpg" />
<input type="checkbox" name="3" value="3" />
</div>
jQuery 或 Javascript 会很棒!谢谢:)
最佳答案
$("img").each(function(){
var id = $(this).attr("title");
$(this).parent().append("<input type='checkbox' name='"+ id +"' value='"+ id +"' />");
});
关于javascript - 在每个 div 中添加带有 img 标题的表单复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12207259/