这是我到目前为止的代码:
var purseValue = 10000;
var storePerson = [];
var storeAmount = [];
$('#prompt').keypress(function() { //
var whatMyUserTyped = $('#prompt').val(); //Input variable defined for future use.
if (event.keyCode == 13) { //If enter is pressed,
if (whatMyUserTyped === 'purse'){ //If used types "purse"
$('#log li:last').text('ShylockBot pulls out his purse containing '+ purseValue+ ' ducat(s)');
} else { //log the phrase with total amount in purse.
var subtractValue = parseInt(whatMyUserTyped.match(/\d+/)); //scan for the number in my string, then turn into a number.
var arrayOfInput = whatMyUserTyped.split(" ");
purseValue = (purseValue - subtractValue); //purse money minus the above sum equals current amount in purse.
storePerson.push(arrayOfInput[1]);
storeAmount.push(); //I tried to use the "subtractValue" but for some reason didn't work. So I just selected from string again.
console.log(storePerson);
console.log(subtractValue);
我正在尝试获取用户输入,我从中输入了他们在字符串中输入的数字,然后我得到了他们的名字(考虑到这句话是:&#34;贷款人50美元&#34;)< / p>
所以person就是arrayOfInput [1],数字就是他们输入的数字。
现在我试图将这些值存储到2个数组中。
我使用了.push(),它适用于&#34; storePerson&#34;每次我的函数运行时,它会将输入人员添加到其中,但是我无法将其添加到数组中。它仅替换一个数字的数组,并且不存储旧值,这意味着将storeAmount替换为最新输入的值。
尽管我是初学者,但我尽力保持清醒。所以我希望有人可以帮助我。感谢。
答案 0 :(得分:1)
我多次阅读,看起来你需要:
storeAmount.push(subtractValue[0]);
//thanks Orleaner for pointing out that match returns an array
你使用双引号吗?
PS:即便如此,它应该不是问题,每次都会添加字符串“subrtractValue”:)
答案 1 :(得分:-1)
要将整数推入jquery中的数组,必须为
storeAmount.push(subtractValue.toString());