Javascript将数组添加到数组对象-推送总是相同值的问题

时间:2018-11-09 10:16:10

标签: javascript jquery html arrays foreach

我对javascript代码有疑问: 我正在创建一个具有动态添加元素的表单,并从该表单中foreach读取值

<div class="product" value="1">
  <br />
  <h4>Pytania dodatkowe lecz obowiązkowe :3</h4>    
  <div class="inputbox left-label required">
    <label for="price" class="rating">price</label>
    <div class="tint-rating" id="" data-input="price0" name="price" value="5"></div>
  </div>
  <div class="inputbox left-label required">
    <label for="functionality" class="rating">Funcionality</label>
    <div class="tint-rating" data-input="functionality0" name="functionality" value="5"></div>
  </div>
  <div class="inputbox left-label required">
    <label for="design" class="rating">Design</label>
    <div class="tint-rating" data-input="design0" name="design" value="5"></div>
  </div>   
</div>    
<div class="product" value="2">
  <br />
  <h4>Pytania dodatkowe lecz obowiązkowe :3</h4>    
  <div class="inputbox left-label required">
    <label for="price" class="rating">price</label>
    <div class="tint-rating" id="" data-input="price1" name="price" value="6"></div>
  </div>
  <div class="inputbox left-label required">
    <label for="functionality" class="rating">Funcionality</label>
    <div class="tint-rating" data-input="functionality1" name="functionality" value="3"></div>
  </div>
  <div class="inputbox left-label required">
    <label for="design" class="rating">Design</label>
    <div class="tint-rating" data-input="design1" name="design" value="8"></div>
  </div>    
</div>    
<div class="product" value="3">
  <br />
  <h4>Pytania dodatkowe lecz obowiązkowe :3</h4>    
  <div class="inputbox left-label required">
    <label for="price" class="rating">price</label>
    <div class="tint-rating" id="" data-input="price0" name="price" value="1"></div>
  </div>
  <div class="inputbox left-label required">
    <label for="functionality" class="rating">Funcionality</label>
    <div class="tint-rating" data-input="functionality1" name="functionality" value="2"></div>
  </div>
  <div class="inputbox left-label required">
    <label for="design" class="rating">Design</label>
    <div class="tint-rating" data-input="design1" name="design" value="3"></div>
  </div>    
</div>

这是代码从表单中读取值:

var arr = [], rate = {};      
products = [{}];

$.each($('.product'), function(i, product) {    
  $('.tint-rating', product).each(function() {
    var x = $(this).attr("name")
       ,y = $(this).attr("value");     
    rate[x] = y;     
  });    
  products.push({rate});    //Move this line into inside the inner .each loop
});

问题是脚本推送到表单元素的数组计数元素,但所有值仅来自最后一个(来自示例):

0: {}
1:
rate: {price: "1", functionality: "2", design: "3"}
__proto__: Object
2:
rate: {price: "1", functionality: "2", design: "3"}
__proto__: Object
3:
rate: {price: "1", functionality: "2", design: "3"}
__proto__: Object
length: 4

我不仅需要表单中所有元素的值,还需要这样,

 0: {}
    1:
    rate: {price: "5", functionality: "5", design: "5"}
    __proto__: Object
    2:
    rate: {price: "6", functionality: "3", design: "8"}
    __proto__: Object
    3:
    rate: {price: "1", functionality: "2", design: "3"}
    __proto__: Object
    length: 4

如果有人知道我的脚本有什么问题,请帮助我, 提前非常感谢您!

1 个答案:

答案 0 :(得分:0)

将代码修改为此:

keytool -importkeystore -srckeystore server.jks -destkeystore server.p12 -srcstoretype JKS -deststoretype PKCS12 -srcalias jwtkey -destalias jwtkey

这应该给您:

    products = [];
    $.each($('.product'), function(i, product) {    
      var item = {};
      $('.tint-rating', product).each(function() {
        item[$(this).attr("name")] = $(this).attr("value");
      });    
      products.push(item);
    });

@Dean Marketing:非常感谢,它有效!! :))