我动态地从db读取值并在DOM中追加元素,我在下面的代码中我正在读取一个字段并将其存储在id例如下面的示例中。然而,表中还有一个字段叫做注释,因为我应该在html元素上设置它,以便我不需要再次触发数据库查询以进行注释?
<div id="selectionbox" style="display:inline-block">
<img style="display:inline-block" id="activity_image11" src="../images/Desert.jpg" width="100" height="100">
<input type="checkbox" class="class1" id="11" style="display:inline-block">
</div>
我上面有很多选择框,我将其附加到DOM中,稍后我将使用选中的复选框,
temp.push($( "div#"+this.id ).find( "input[type=checkbox]:checked" ).map(function() {
return this.id
}).get());
我的问题是,如果我将存储评论in data-value="10"
,那么我该如何获取这些数据字段?
答案 0 :(得分:1)
如果我理解正确,您正在尝试从data-value
您可以简单地获取数据值,例如
$('#comments').data('value');
或
$('#comments').attr('data-value');
答案 1 :(得分:1)
您可以在复选框[data-value]中添加一个额外属性。您的代码将如下所示:
<div id="selectionbox" style="display:inline-block">
<img style="display:inline-block" id="activity_image11" src="../images/Desert.jpg" width="100" height="100">
<input type="checkbox" class="class1" id="11" data-comment="some comments for 11" style="display:inline-block">
<input type="checkbox" class="class1" id="12" data-comment="some comments for 12" style="display:inline-block">
<input type="checkbox" class="class1" id="13" data-comment="BC" style="display:inline-block">
</div>
之后您可以从复选框中获取此值:
$("#selectionbox input[type=checkbox]:checked").attr("data-comment");
P.S。如果评论数据很大,那么我建议使用jQuery data
而不是使用新属性