循环获取属性值

时间:2018-03-26 00:16:35

标签: javascript jquery html

我正在尝试创建一个循环,它将从某些按钮获取某些类的数据。 这就是我的HTML的样子:

while($("see").attr("price")) {
    if($("see").attr("price") + 5 > 10) {
        alert("hello");
    }
}

我想要一个可以做这样的事情的循环:

{{1}}

我想通过按钮循环,并在按钮"价格"属性大于例如10。

对于糟糕的描述感到抱歉,我无法解释它。

1 个答案:

答案 0 :(得分:1)

您需要遍历按钮。请使用函数.serialize并将每个属性转换为数字each

此选择器price获取包含类.see[price]且属性为see的元素。



price

$(".see[price]").each(function() {
  if (+$(this).attr("price") + 5 > 10) {
    console.log("hello");
  }
});




我建议您将<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button class="see" price="5">First</button> <button class="see" price="8">Second</button> <button class="see" price="10">Third</button>用于未知属性:

&#13;
&#13;
data-attributes
&#13;
$(".see[data-price]").each(function() {
  if (+$(this).data("price") + 5 > 10) {
    console.log("hello");
  }
});
&#13;
&#13;
&#13;

相关问题