如何使用 Jquery 将属性值转换为整数?
How can I convert value of attribute into a integer using Jquery?
我创建了一个函数,用于查找属性值并输出平均值。现在,这是行不通的。我相信这与我选择属性的方式有关。
这里是 fiddle - https://jsfiddle.net/carbot3000/0kjLu3h0/7/
我需要帮助来确定我做错了什么。更新后的 fiddle 也会很有帮助。
var total = 0,
valid_labels = 0,
average;
var reviewAverage = $('meta[itemprop="ratingValue"]').attr('content');
$(reviewAverage).each(function () {
var val = parseInt(reviewAverage), 10);
if (val !== 0) {
valid_labels += 1;
total += val;
}
});
average = total / valid_labels;
$(".reviewRating").val(average);
您的 HTML 片段:
<meta content="5" itemprop="ratingValue">
加上这一行
var reviewAverage = $('meta[itemprop="ratingValue"]').attr('content');
您正在从 $('meta[itemprop="ratingValue"]')
接收 jQuery 个对象的集合,然后尝试 运行 为单个对象构建的方法。此外,您的 content
属性是一个数字,因此 运行ning $(5)
也无济于事。
您可以使用 $.map
将您的所有评分放入常规数组 - 然后 运行 一个 reduce
函数:
var reviewRatings = $('meta[itemprop="ratingValue"]').map(function() {
return $(this).attr("content");
}).get();
var reviewAverages = reviewRatings.reduce(function(total, rating) {
return total += parseInt(rating, 10);
}, 0) / reviewRatings.length;
就像tymeJV说的。您尝试对集合使用 "attr" 方法。这需要按照您希望的方式工作。如果您想更贴近您的代码并仅更改代码段的 2 行,您可以这样做:
var numItems = $('div[itemprop="reviewRating"]').length;
$('.reviewCount').html(numItems);
var total = 0,
valid_labels = 0,
average;
var reviewAverage = $('meta[itemprop="ratingValue"]');
$(reviewAverage).each(function () {
var val = parseInt($(this).attr('content'), 10);
if (val !== 0) {
valid_labels += 1;
total += val;
}
});
average = total / valid_labels;
alert(average);
$(".reviewRating").val(average);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Customer Reviews<div itemscope itemtype="http://schema.org/Review"> <div itemprop="itemReviewed" itemscope itemtype="http://schema.org/LocalBusiness"> </div> <div itemprop="reviewRating" itemscope="" itemtype="http://schema.org/Rating"><meta content="5" itemprop="ratingValue"> <meta content="0.0" itemprop="worstRating"> <meta content="5.0" itemprop="bestRating"> <span class="star" style="color:gold;">★★★★★</span></div><div><span itemprop="reviewBody">"Everybody was wonderful!! Very accommodating. You have a terrific staff at the Norwalk location. Please reward them well. I was pleasantly surprised with the attention to detail and"</span> <div itemprop="author" itemscope itemtype="http://schema.org/Person">Submitted by: <span itemprop="name">Jenelle</span></div></div></div><div itemscope itemtype="http://schema.org/Review"> <div itemprop="itemReviewed" itemscope itemtype="http://schema.org/LocalBusiness"> </div> <div itemprop="reviewRating" itemscope="" itemtype="http://schema.org/Rating"><meta content="5" itemprop="ratingValue"> <meta content="0.0" itemprop="worstRating"> <meta content="5.0" itemprop="bestRating"> <span class="star" style="color:gold;">★★★★★</span></div><div><span itemprop="reviewBody">"The staff (Elvis and John) who supervised our party at the Norwalk location were great. They were fun (wearing party hats during the party) and were very nice to the children, to us and to our guests. They were enthusiastic and were very accommodating and helpful. They even played air hockey with my son which made him very happy! I highly commend Elvis and John as party hosts. I also commend the staff at the front desk who were very nice and helpful as well."</span> <div itemprop="author" itemscope itemtype="http://schema.org/Person">Submitted by: <span itemprop="name">Jennifer</span></div></div></div><div itemscope itemtype="http://schema.org/Review"> <div itemprop="itemReviewed" itemscope itemtype="http://schema.org/LocalBusiness"> </div> <div itemprop="reviewRating" itemscope="" itemtype="http://schema.org/Rating"><meta content="4.5" itemprop="ratingValue"> <meta content="0.0" itemprop="worstRating"> <meta content="5.0" itemprop="bestRating"> <span class="star" style="color:gold;">★★★★★</span></div><div><span itemprop="reviewBody">"The two team member that helped with Emily's birthday, Elvis plus one other man (can't remember his name) were both excellent and really made a great effort to make sure Emily was having a great time"</span> <div itemprop="author" itemscope itemtype="http://schema.org/Person">Submitted by: <span itemprop="name">Emma</span></div></div></div><div itemscope itemtype="http://schema.org/Review"> <div itemprop="itemReviewed" itemscope itemtype="http://schema.org/LocalBusiness"> </div> <div itemprop="reviewRating" itemscope="" itemtype="http://schema.org/Rating"><meta content="4.5" itemprop="ratingValue"> <meta content="0.0" itemprop="worstRating"> <meta content="5.0" itemprop="bestRating"> <span class="star" style="color:gold;">★★★★★</span></div><div><span itemprop="reviewBody">"This is my second experience with Pump It Up. The children enjoyed themselves tremendously.Loved the experience from the children activity as well as the food."</span> <div itemprop="author" itemscope itemtype="http://schema.org/Person">Submitted by: <span itemprop="name">Carolyn</span></div></div></div><div itemscope itemtype="http://schema.org/Review"> <div itemprop="itemReviewed" itemscope itemtype="http://schema.org/LocalBusiness"> </div> <div itemprop="reviewRating" itemscope="" itemtype="http://schema.org/Rating"><meta content="5" itemprop="ratingValue"> <meta content="0.0" itemprop="worstRating"> <meta content="5.0" itemprop="bestRating"> <span class="star" style="color:gold;">★★★★★</span></div><div><span itemprop="reviewBody">"Great energy and cordiality towards family and guest!"</span> <div itemprop="author" itemscope itemtype="http://schema.org/Person">Submitted by: <span itemprop="name">Benny</span></div></div></div><div itemscope itemtype="http://schema.org/Review"> <div itemprop="itemReviewed" itemscope itemtype="http://schema.org/LocalBusiness"> </div> <div itemprop="reviewRating" itemscope="" itemtype="http://schema.org/Rating"><meta content="4" itemprop="ratingValue"> <meta content="0.0" itemprop="worstRating"> <meta content="5.0" itemprop="bestRating"> <span class="star" style="color:gold;">★★★★</span></div><div><span itemprop="reviewBody">"The manager was very accommodating. He even helped us open the giveaways which were still in boxes. Sometimes parents just can't do everything and need to enjoy the party as well and this is where staff and managers come in to help. Kudos!"</span> <div itemprop="author" itemscope itemtype="http://schema.org/Person">Submitted by: <span itemprop="name">Patricia</span></div></div></div><div itemscope itemtype="http://schema.org/Review"> <div itemprop="itemReviewed" itemscope itemtype="http://schema.org/LocalBusiness"> </div> <div itemprop="reviewRating" itemscope="" itemtype="http://schema.org/Rating"><meta content="4" itemprop="ratingValue"> <meta content="0.0" itemprop="worstRating"> <meta content="5.0" itemprop="bestRating"> <span class="star" style="color:gold;">★★★★</span></div><div><span itemprop="reviewBody">"I liked that a staff person came to my car with a cart and took everything from me to set up party room."</span> <div itemprop="author" itemscope itemtype="http://schema.org/Person">Submitted by: <span itemprop="name">Lisa</span></div></div></div><div itemscope itemtype="http://schema.org/Review"> <div itemprop="itemReviewed" itemscope itemtype="http://schema.org/LocalBusiness"> </div> <div itemprop="reviewRating" itemscope="" itemtype="http://schema.org/Rating"><meta content="5" itemprop="ratingValue"> <meta content="0.0" itemprop="worstRating"> <meta content="5.0" itemprop="bestRating"> <span class="star" style="color:gold;">★★★★★</span></div><div><span itemprop="reviewBody">"Jesse and the entire team ( i can't remember their names) and the very nice young lady at the register (she has twins). Exceptional team you have there. Please recognize them!! They made our children's birthday party experience very memorable. I left there feeling so happy. Thank you!! Please continue to train all of your new staff like you have trained them"</span> <div itemprop="author" itemscope itemtype="http://schema.org/Person">Submitted by: <span itemprop="name">Jenelle</span></div></div></div>
<div class="reviewCount">
</div>
<div class="reviewRating">
</div>
我创建了一个函数,用于查找属性值并输出平均值。现在,这是行不通的。我相信这与我选择属性的方式有关。
这里是 fiddle - https://jsfiddle.net/carbot3000/0kjLu3h0/7/
我需要帮助来确定我做错了什么。更新后的 fiddle 也会很有帮助。
var total = 0,
valid_labels = 0,
average;
var reviewAverage = $('meta[itemprop="ratingValue"]').attr('content');
$(reviewAverage).each(function () {
var val = parseInt(reviewAverage), 10);
if (val !== 0) {
valid_labels += 1;
total += val;
}
});
average = total / valid_labels;
$(".reviewRating").val(average);
您的 HTML 片段:
<meta content="5" itemprop="ratingValue">
加上这一行
var reviewAverage = $('meta[itemprop="ratingValue"]').attr('content');
您正在从 $('meta[itemprop="ratingValue"]')
接收 jQuery 个对象的集合,然后尝试 运行 为单个对象构建的方法。此外,您的 content
属性是一个数字,因此 运行ning $(5)
也无济于事。
您可以使用 $.map
将您的所有评分放入常规数组 - 然后 运行 一个 reduce
函数:
var reviewRatings = $('meta[itemprop="ratingValue"]').map(function() {
return $(this).attr("content");
}).get();
var reviewAverages = reviewRatings.reduce(function(total, rating) {
return total += parseInt(rating, 10);
}, 0) / reviewRatings.length;
就像tymeJV说的。您尝试对集合使用 "attr" 方法。这需要按照您希望的方式工作。如果您想更贴近您的代码并仅更改代码段的 2 行,您可以这样做:
var numItems = $('div[itemprop="reviewRating"]').length;
$('.reviewCount').html(numItems);
var total = 0,
valid_labels = 0,
average;
var reviewAverage = $('meta[itemprop="ratingValue"]');
$(reviewAverage).each(function () {
var val = parseInt($(this).attr('content'), 10);
if (val !== 0) {
valid_labels += 1;
total += val;
}
});
average = total / valid_labels;
alert(average);
$(".reviewRating").val(average);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Customer Reviews<div itemscope itemtype="http://schema.org/Review"> <div itemprop="itemReviewed" itemscope itemtype="http://schema.org/LocalBusiness"> </div> <div itemprop="reviewRating" itemscope="" itemtype="http://schema.org/Rating"><meta content="5" itemprop="ratingValue"> <meta content="0.0" itemprop="worstRating"> <meta content="5.0" itemprop="bestRating"> <span class="star" style="color:gold;">★★★★★</span></div><div><span itemprop="reviewBody">"Everybody was wonderful!! Very accommodating. You have a terrific staff at the Norwalk location. Please reward them well. I was pleasantly surprised with the attention to detail and"</span> <div itemprop="author" itemscope itemtype="http://schema.org/Person">Submitted by: <span itemprop="name">Jenelle</span></div></div></div><div itemscope itemtype="http://schema.org/Review"> <div itemprop="itemReviewed" itemscope itemtype="http://schema.org/LocalBusiness"> </div> <div itemprop="reviewRating" itemscope="" itemtype="http://schema.org/Rating"><meta content="5" itemprop="ratingValue"> <meta content="0.0" itemprop="worstRating"> <meta content="5.0" itemprop="bestRating"> <span class="star" style="color:gold;">★★★★★</span></div><div><span itemprop="reviewBody">"The staff (Elvis and John) who supervised our party at the Norwalk location were great. They were fun (wearing party hats during the party) and were very nice to the children, to us and to our guests. They were enthusiastic and were very accommodating and helpful. They even played air hockey with my son which made him very happy! I highly commend Elvis and John as party hosts. I also commend the staff at the front desk who were very nice and helpful as well."</span> <div itemprop="author" itemscope itemtype="http://schema.org/Person">Submitted by: <span itemprop="name">Jennifer</span></div></div></div><div itemscope itemtype="http://schema.org/Review"> <div itemprop="itemReviewed" itemscope itemtype="http://schema.org/LocalBusiness"> </div> <div itemprop="reviewRating" itemscope="" itemtype="http://schema.org/Rating"><meta content="4.5" itemprop="ratingValue"> <meta content="0.0" itemprop="worstRating"> <meta content="5.0" itemprop="bestRating"> <span class="star" style="color:gold;">★★★★★</span></div><div><span itemprop="reviewBody">"The two team member that helped with Emily's birthday, Elvis plus one other man (can't remember his name) were both excellent and really made a great effort to make sure Emily was having a great time"</span> <div itemprop="author" itemscope itemtype="http://schema.org/Person">Submitted by: <span itemprop="name">Emma</span></div></div></div><div itemscope itemtype="http://schema.org/Review"> <div itemprop="itemReviewed" itemscope itemtype="http://schema.org/LocalBusiness"> </div> <div itemprop="reviewRating" itemscope="" itemtype="http://schema.org/Rating"><meta content="4.5" itemprop="ratingValue"> <meta content="0.0" itemprop="worstRating"> <meta content="5.0" itemprop="bestRating"> <span class="star" style="color:gold;">★★★★★</span></div><div><span itemprop="reviewBody">"This is my second experience with Pump It Up. The children enjoyed themselves tremendously.Loved the experience from the children activity as well as the food."</span> <div itemprop="author" itemscope itemtype="http://schema.org/Person">Submitted by: <span itemprop="name">Carolyn</span></div></div></div><div itemscope itemtype="http://schema.org/Review"> <div itemprop="itemReviewed" itemscope itemtype="http://schema.org/LocalBusiness"> </div> <div itemprop="reviewRating" itemscope="" itemtype="http://schema.org/Rating"><meta content="5" itemprop="ratingValue"> <meta content="0.0" itemprop="worstRating"> <meta content="5.0" itemprop="bestRating"> <span class="star" style="color:gold;">★★★★★</span></div><div><span itemprop="reviewBody">"Great energy and cordiality towards family and guest!"</span> <div itemprop="author" itemscope itemtype="http://schema.org/Person">Submitted by: <span itemprop="name">Benny</span></div></div></div><div itemscope itemtype="http://schema.org/Review"> <div itemprop="itemReviewed" itemscope itemtype="http://schema.org/LocalBusiness"> </div> <div itemprop="reviewRating" itemscope="" itemtype="http://schema.org/Rating"><meta content="4" itemprop="ratingValue"> <meta content="0.0" itemprop="worstRating"> <meta content="5.0" itemprop="bestRating"> <span class="star" style="color:gold;">★★★★</span></div><div><span itemprop="reviewBody">"The manager was very accommodating. He even helped us open the giveaways which were still in boxes. Sometimes parents just can't do everything and need to enjoy the party as well and this is where staff and managers come in to help. Kudos!"</span> <div itemprop="author" itemscope itemtype="http://schema.org/Person">Submitted by: <span itemprop="name">Patricia</span></div></div></div><div itemscope itemtype="http://schema.org/Review"> <div itemprop="itemReviewed" itemscope itemtype="http://schema.org/LocalBusiness"> </div> <div itemprop="reviewRating" itemscope="" itemtype="http://schema.org/Rating"><meta content="4" itemprop="ratingValue"> <meta content="0.0" itemprop="worstRating"> <meta content="5.0" itemprop="bestRating"> <span class="star" style="color:gold;">★★★★</span></div><div><span itemprop="reviewBody">"I liked that a staff person came to my car with a cart and took everything from me to set up party room."</span> <div itemprop="author" itemscope itemtype="http://schema.org/Person">Submitted by: <span itemprop="name">Lisa</span></div></div></div><div itemscope itemtype="http://schema.org/Review"> <div itemprop="itemReviewed" itemscope itemtype="http://schema.org/LocalBusiness"> </div> <div itemprop="reviewRating" itemscope="" itemtype="http://schema.org/Rating"><meta content="5" itemprop="ratingValue"> <meta content="0.0" itemprop="worstRating"> <meta content="5.0" itemprop="bestRating"> <span class="star" style="color:gold;">★★★★★</span></div><div><span itemprop="reviewBody">"Jesse and the entire team ( i can't remember their names) and the very nice young lady at the register (she has twins). Exceptional team you have there. Please recognize them!! They made our children's birthday party experience very memorable. I left there feeling so happy. Thank you!! Please continue to train all of your new staff like you have trained them"</span> <div itemprop="author" itemscope itemtype="http://schema.org/Person">Submitted by: <span itemprop="name">Jenelle</span></div></div></div>
<div class="reviewCount">
</div>
<div class="reviewRating">
</div>