javascript - 在每个 div 中添加带有 img 标题的表单复选框

标签 javascript jquery html forms

如何通过增加 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/

相关文章:

javascript设置高度

javascript - safari 无法在鼠标单击时执行 js

javascript - 如何在alert中打印数组数据

javascript - 下拉列表未填充 - Angular JS

javascript - 处理垂直文本区域

html - Windows 如何将 HTML 文件链接到文件夹?

javascript - axios.all 传播并捕获所有

javascript - 有没有办法确定在 Firebug 或网络检查器中发出 XHR 的行?

html - 水平导航栏子导航向下环绕而不是使用它后面(左)的空间

css - 欢迎来到匿名 HTML 表格