简体   繁体   English

遍历jQuery数组/将值输出到div

[英]Loop through jQuery arrays / Output values to a div

I have a button that dynamically creates 2 inputs per click 我有一个按钮,每次点击可动态创建2个输入

Data of Input 1: string Data of Input 2: number (float) (0-100) 输入1的 数据 字符串输入2的数据:数字(浮点数)(0-100)

I am creating an array of each like this. 我正在创建每个这样的数组。

var recipe_flavour_name = $("input[name='flav-name']").map(function()  {return $(this).val();}).get();
var recipe_flavour_percent = $("input[name='flav-percent']").map(function(){return $(this).val();}).get();

As far as I can tell, the arrays are comma separated values. 据我所知,数组是逗号分隔的值。

Let's for simplicity's sake say: 为了简单起见,让我们说:

recipe_flavour_name = a,b,c
recipe_flavour_percent = 5,6,7

I then want to take the number value to use in a function and then loop through all the values and use jQuery's .html() to add the values to a div . 然后,我想获取要在函数中使用的number value ,然后遍历所有值,然后使用jQuery的.html()将值添加到div

I have tried this: flavPercent1 is just recipe_flavour_percent 我已经试过了: flavPercent1只是recipe_flavour_percent

var arrayLength = flavPercent1.Length;

for (var i = 0; i < arrayLength; i++) {
    var flavML = (flavPercent1[0] / 100 * 100 * 1000) / 1000;
    var flavGrams = (flavML * .98 * 100) / 100;
    var flavPercent = (flavML / 100 * 1E4) / 100;
    $('.live-flavours').html('<tr>'+flavName[0]+'<td></td>'+parseFloat(flavML).toFixed(2)+'<td>'+parseFloat(flavGrams).toFixed(2)+'</td><td>'+parseFloat(flavPercent1[0]).toFixed(2)+'</td></tr>');
};

But I only get flavGrams and flavPercent returned, dynamically adding more data to the array does nothing. 但是我只返回了flavGrams和flavPercent,将更多数据动态添加到数组中没有任何作用。

What do I want to achieve? 我想实现什么?

  • Grab the values of specified inputs in an array. 抓取数组中指定输入的值。
  • Pass them to a function. 将它们传递给函数。
  • Loop through the values and output them in HTML using jQuery. 循环遍历这些值,然后使用jQuery将其输出为HTML。

I hope that makes sense and thanks in advance. 我希望这是有道理的,并预先感谢。

Ok, so assuming that you don't have a problem getting the arrays you need, the problem lies within your for loop. 好的,所以假设您在获取所需的数组时没有问题,问题就出在您的for循环内。

YOUR CODE: 您的代码:

for (var i = 0; i < arrayLength; i++) {
var flavML = (flavPercent1[0] / 100 * AMOUNT * 1000) / 1000;
var flavGrams = (flavML * .98 * 100) / 100;
var flavPercent = (flavML / AMOUNT * 1E4) / 100;
$('.live-flavours').html('<tr>'+flavName[0]+'<td></td>'+parseFloat(flavML).toFixed(2)+'<td>'+parseFloat(flavGrams).toFixed(2)+'</td><td>'+parseFloat(flavPercent1[0]).toFixed(2)+'</td></tr>');};
  1. You put everything in the for loop, yet make no reference to the index. 您将所有内容放入for循环中,但未引用该索引。 I'm assuming everywhere you put [0] you actually want [i]. 我假设到处都是您实际需要的[0]。 This means that every time the index increases, you are getting the next array element. 这意味着每当索引增加时,您将获得下一个数组元素。

    1. You should use .append instead of .html. 您应该使用.append而不是.html。 .html means that the current html will be replaced by what you are adding. .html表示当前的html将被您添加的内容替换。

    2. Finally, although making it dynamic is possible, I'm not sure that JQuery is the best libary to use in this case. 最后,尽管可以动态化它,但我不确定在这种情况下JQuery是最好的库。 I'd suggest taking a look at libraries such as Vue or MoonJs (both are very light and very simple libraries) etc... to find a much easier, and frankly better way to do this. 我建议您看一下Vue或MoonJs之类的库(都是非常轻便且非常简单的库)等...,以找到一种更简单,坦率地说更好的方法。 They allow for dynamic rendering, and what you are trying to do becomes insanely simplified. 它们允许动态渲染,并且您正在尝试做的事情变得异常简化。

Hope this helps. 希望这可以帮助。

(hopefully) WORKING CODE: (希望如此)工作代码:

 for (var i = 0; i < arrayLength; i++) {
    var flavML = (flavPercent1[i] / 100 * AMOUNT * 1000) / 1000;
    var flavGrams = (flavML * .98 * 100) / 100;
    var flavPercent = (flavML / AMOUNT * 1E4) / 100;
    $('.live-flavours').append('<tr>'+flavName[i]+'<td></td>'+parseFloat(flavML).toFixed(2)+'<td>'+parseFloat(flavGrams).toFixed(2)+'</td><td>'+parseFloat(flavPercent1[i]).toFixed(2)+'</td></tr>');};

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

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