javascript 如何在数组中添加输入值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6702883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 21:35:32  来源:igfitidea点击:

how to add the input values in an array

javascriptarraysjson

提问by jayAnn

i just like to ask regarding adding data in a array. But the data which i wanted to put is from a table of input boxes.. Here's the code that i've been practicing to get data:

我只是想问关于在数组中添加数据。但是我想放入的数据来自输入框表..这是我一直在练习获取数据的代码:

http://jsfiddle.net/yajeig/4Nr9m/69/

http://jsfiddle.net/yajeig/4Nr9m/69/

I have an add button that everytime I click that button, it will store data in my_datavariable.

我有一个添加按钮,每次单击该按钮时,它都会将数据存储在my_data变量中。

i want to produce an output in my variable something like this:

我想在我的变量中产生这样的输出:

my_data  = [ {plank:"1",thickness:"4",width:"6",length:"8",qty:"1",brdFt:"16"}] 

and if i would add another data again, it will add in that variable and it be something like this:

如果我要再次添加另一个数据,它会添加该变量,它是这样的:

my_data  = [ {plank:"1",thickness:"4",width:"6",length:"8",qty:"1",brdFt:"16"},
     {plank:"2",thickness:"5",width:"6",length:"2",qty:"1",brdFt:"50"}] 

the code that i have right now is really bad, so please help.

我现在拥有的代码真的很糟糕,所以请帮忙。

Currently my output:1,4,6,4,1

目前我的输出:1,4,6,4,1

采纳答案by detaylor

You should be able to iterate over all of the textboxes using the following:

您应该能够使用以下方法遍历所有文本框:

function add(e) {
    var obj = {};

    $('#addItem input[type="text"]')
        .each(function(){obj[this.name] = this.value;});
    myItems.push(obj);
}

Where myItemsis a global container for your items and #addItemis your form.

哪里myItems是您的项目的全局容器,#addItem是您的表单。

Updated jsfiddle.

更新了jsfiddle

If you use a form and a submit button then you should be able to implement a non-JavaScript method to add your information so that the site will be accessible to people without JavaScript enabled.

如果您使用表单和提交按钮,那么您应该能够实现一种非 JavaScript 方法来添加您的信息,以便没有启用 JavaScript 的人可以访问该站点。

回答by Anthony Simmon

Try this, sorry for modifying your form, but it works well:

试试这个,抱歉修改你的表单,但它运行良好:

HTML:

HTML:

<form method="post" action="#" id="add_plank_form">
    <p><label for="plank_number">Plank number</label>
    <p><input type="text" name="plank_number" id="plank_number"/></p>

    <p><label for="plank_width">Width</label>
    <p><input type="text" name="plank_width" id="plank_width"/></p>

    <p><label for="plank_length">Length</label>
    <p><input type="text" name="plank_length" id="plank_length"/></p>

    <p><label for="plank_thickness">Thickness</label>
    <p><input type="text" name="plank_thickness" id="plank_thickness"/></p>

    <p><label for="plank_quantity">Quantity</label>
    <p><input type="text" name="plank_quantity" id="plank_quantity"/></p>

    <p><input type="submit" value="Add"/>
</form>

<p id="add_plank_result"></p>

Javascript:

Javascript:

$(document).ready(function() {

    var plank_data = Array();

    $('#add_plank_form').submit(function() {

        // Checking data
        $('#add_plank_form input[type="text"]').each(function() {
            if(isNaN(parseInt($(this).val()))) {
                return false;
            }
        });

        var added_data = Array();
        added_data.push(parseInt($('#plank_number').val()));
        added_data.push(parseInt($('#plank_width').val()));
        added_data.push(parseInt($('#plank_length').val()));
        added_data.push(parseInt($('#plank_thickness').val()));
        added_data.push(parseInt($('#plank_quantity').val()));

        $('#add_plank_form input[type="text"]').val('');

        plank_data.push(added_data);

        // alert(JSON.stringify(plank_data));

        // compute L x W x F for each plank data
        var computed_values = Array();
        $('#add_plank_result').html('');
        for(var i=0; i<plank_data.length; i++) {
            computed_values.push(plank_data[i][1] * plank_data[i][2] * plank_data[i][3] / 12);
            $('#add_plank_result').append('<input type="text" name="plank_add[]" value="' + computed_values[i] + '"/>');
        }

        return false;
    });
});

回答by mplungjan

Sorry I only glanced at the other solutions.

抱歉,我只看了一眼其他解决方案。

$(document).ready(function() {
 var myData=[];
    var myObject = {} 
        $("input").each(function() {
            myObject[this.id]=this.value
        });
     alert(myObject["plank"])
   myData.push(myObject)
});

回答by Ming-Tang

Iterate through all keys, and add the values. (code written from mind, not tested)

遍历所有键,并添加值。(代码是凭头脑写的,未经测试)

var added = { };
for (var i = 0; i < my_data.length; i ++) {
  var json = my_data[i];
  for (var key in json) {
    if (json.hasOwnProperty(key)) {
      if (key in added) {
        added[key] += json[key];
      } else {
        added[key] = json[key];
      }
    }
  }
}

回答by Anthony Simmon

You can use the javascript array push function:

您可以使用javascript 数组推送功能

var data = [{plank:"1",thickness:"4",width:"6",length:"8",qty:"1",brdFt:"16"}];

var to_add = [{plank:"2",thickness:"5",width:"6",length:"2",qty:"1",brdFt:"50"}];

data = data.concat(to_add);