【发布时间】:2017-08-12 08:13:34
【问题描述】:
基本上,我想要做的是将数字输入更改添加到出版物列表中。问题是每次我更改输入时都会保留以前的出版物和出版物的数量。例如:
我点击第一个输入 2x,这就是我收到的内容:
出版物 1:数量:1
出版物1:数量:2
应该发生的是,当您单击输入时,它将覆盖以前的数量。比如:
出版物 1:数量:1
出版物 1:数量 2
出版物 2:数量 1
注意删除线。那不应该再存在了。数量已更新。 编解码器
http://codepen.io/Jesders88/pen/evVrrw
HTML
<input type="number" data-name="something here" data-qty="1" data-id="1">
<input type="number" data-name="something else" data-qty="3" data-id="2">
<input type="number" data-name="something other" data-qty="5" data-id="3">
JAVASCRIPT
publications = new Array;
$('input').on('change', function(e){
e.preventDefault();
var pid = parseInt($(this).data('id')); // id of thing
var name = $(this).data('name'); // name of thing
var qty = parseInt($(this).data('qty'));
console.log(pid);
console.log(name);
console.log(qty);
if(typeof publications[pid] == 'undefined')
{
publications[pid] = new Array;
publications[pid][0] = name;
publications[pid][1] = qty;
}
else
{
publications[pid][1] = qty;
}
console.log(publications);
$.each(publications, function(i, l){
//console.log( "Index #" + i + ": " + l );
console.log(l[0]+' has a qty of: '+l[1]);
});
});
【问题讨论】:
标签: javascript jquery html arrays associative-array