我想用输入字段更新数组,而是创建一个新数组

时间:2016-05-20 13:17:12

标签: javascript arrays push

我想通过在输入字段中键入需要更新的内容来更新我的数组。但不是更新它而是创建一个新的数字。 我该如何解决这个问题?

Whats it like before i changed it (console to the right)

After it changes (console to the right)

以下是代码:

function appendInputs(param) {
  param = param || '';
  var inputfield = $('div.words div div div input.inputrule').last();

  if (inputfield.length == 0) {
    var amount = 0;
  }
  else {
    var amount = inputfield.data('id') + 1;
  }
  //De vakjes van Cms.php
  var html = '<div class="change"> <div class="col-md-2"> <input type="number" value='+mp3[param-1]+' class="form-control" id="inp' + result + '" oninput="magic(this.id)"></div> </div> <div class="form-group exerciserule" id="' + amount + '"> \
                <div class="col-xs-10"><input id="exercise_details_rule' + amount + '" name="rules" data-id="' + amount + '" class="form-control inputrule" value="' +nummer_text[param-1]+ '" type="text"> \
              </div> </div>';
  result++;


  //append html
  $('#textList').append(html);

  $('#exercise_details_rule' + amount).focus();
} 

function magic(id) {
  var solo = document.getElementById(""+ id +"").value;

  var string = id;
  var index = string.slice(string.length - 1, string.length);
  //mp3[value] = solo;
  mp3.push(solo);
  console.log(mp3);
}

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

试试这个:

string.slice(string.length - 1, 1, string.length);

这将删除最后一个元素并将新元素添加到数组的末尾

但我不确定你理解你需要什么......