简体   繁体   English

在每个函数中使用jquery数据属性

[英]Using jquery data attributes with each function

I'm building a quote generator, and there is a product field where a user can select a product, select the quantity, then add another if they wish. 我正在构建报价生成器,并且有一个产品字段,用户可以在其中选择产品,选择数量,然后根据需要添加其他产品。

I'm using an each function to loop through all the products they add to sum the price. 我正在使用每个功能来遍历它们添加的所有产品以求和。

For regular values, my JS is running great, but I want to add a second price (minimum price) that the product can be sold for. 对于常规值,我的JS运行良好,但我想添加可以出售产品的第二个价格(最低价格)。 I've added the data as an attribute and i'm trying to use the same method to pull the price from the attribute, but it just keeps returning 'undefined'!!!! 我已经将数据添加为属性,并且我试图使用相同的方法从属性中获取价格,但是它只会不断返回“未定义”!

HTML 的HTML

<select class="form-control onChangePrice system1" name="SystemModel">
  <option value="">Select...</option>
  <option value="3300" data-min-price="3000">System 1</option>
  <option value="4500" data-min-price="4000">System 2</option>
  <option value="6000" data-min-price="5500">System 3</option>
  <option value="6000" data-min-price="5500">System 4</option>
</select>
<div class="col-lg-3 col-md-3 col-sm-3">
  <input class="form-control onChangePrice systemNumber" type="number" name="SystemModelAmount" value="1">
</div>

JS JS

 var systemTotal = 0;
 var systemMin = 0;
 var i = 0;
 $('.system1').each(function(){
  if (this.value != "") {
    systemEachMin = $(this).data("minPrice");
    console.log(systemEachMin);
    systemEachTotal = this.value * parseInt($(".systemNumber").eq(i).val());
    systemTotal += parseFloat(systemEachTotal);
    systemMin += parseFloat(systemEachMin);
  };
  i++;
});

The code works flawlessly for the regular value of the option, i just cant get it to repeat for the data attribute! 该代码对于该选项的常规值而言是完美无缺的,我只是无法让它对data属性重复!

Thanks 谢谢

You're doing a couple of things slightly wrong here: 您在这里做的一些事情有些错误:

$('.system1').each(function(){

should be: 应该:

$('.system1 option').each(function(){

and

systemEachMin = $(this).data("minPrice");

should be: 应该:

systemEachMin = $(this).data("min-price");

So in full: 因此完整:

var systemTotal = 0;
 var systemMin = 0;
 var i = 0;
 $('.system1 option').each(function(){
  if (this.value != "") {
    systemEachMin = $(this).data("min-price");
    console.log(systemEachMin);
    systemEachTotal = this.value * parseInt($(".systemNumber").eq(i).val());
    systemTotal += parseFloat(systemEachTotal);
    systemMin += parseFloat(systemEachMin);
  };
  i++;
});

$(this).data("minPrice"); $(this).data(“ minPrice”); refers to the select tag and not the options tag, there is no data-min-price on the select tag. 指的是选择标签,而不是选项标签,选择标签上没有数据最低价格。

this.options will return all the options in an array this.options将返回数组中的所有选项

or you could use the following for the selected options data attribute 或者您可以对选定的选项数据属性使用以下内容

$(this).find('option:selected').data("minPrice") $(this).find('option:selected')。data(“ minPrice”)

or 要么

$("option:selected", this).data("minPrice") $(“ option:selected”,this).data(“ minPrice”)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM