嘿,我有一些叫做“lp-slider”的jquery滑块设置如下:
<div class="xs-col-12">
<div class="col-xs-2 label label-default">Grit</div>
<div class="col-xs-9">
<div class="lp-slider" data-lpScore="30"></div>
</div>
<div class="col-xs-1"><img class="slider-img" src="images/close-white.png"></div>
<br class="clear-fix">
</div>
<div class="xs-col-12 spacer-med">
<div class="col-xs-2 label label-default">Self-efficacy</div>
<div class="col-xs-9">
<div class="lp-slider" data-lpScore="30"></div>
</div>
<div class="col-xs-1"><img class="slider-img" src="images/close-white.png"></div>
<br class="clear-fix">
</div>
<div class="xs-col-12 spacer-med">
<div class="col-xs-2 label label-default">Sociability</div>
<div class="col-xs-9">
<div class="lp-slider" data-lpScore="30"></div>
</div>
<div class="col-xs-1"><img class="slider-img" src="images/close-white.png"></div>
<br class="clear-fix">
</div>
我想在它们上运行每个语句,以便它抓取data属性并使用它来设置滑块值。我试过这个,我似乎无法弄清楚如何让它工作。它设置了滑块,但值部分无法正常工作:
$(".lp-slider").each(function() {
var value = $(this).data("lpScore");
$(this).slider({
value:value,
range:"min",
min:1,
max:100,
step:1
});
});
答案 0 :(得分:6)
要使用jQuery data()
获取数据属性,您必须使用小写lpscore
来编写它。
以下是对数据Using data attributes with jQuery
应用的规则的深入解释代码:
$(".lp-slider").each(function () {
var value = $(this).data("lpscore");
$(this).slider({
value: value,
range: "min",
min: 1,
max: 100,
step: 1
});
});