我试着用输入元素做一个小计算器。我已经构建了一个加号按钮,在最后一个输入字段之后插入一个新的输入字段。起作用了。
现在我试着对每个输入字段进行求和。但是我在控制台中得到了未定义的。我不知道出了什么问题,因为我刚开始编程。我尝试用. input类获取输入字段的值,然后将其放入数组中。然后做一个for循环来加和它们。
编辑:
非常感谢你的帮助。我使用@Yoiki的代码是因为它是一种新的、简单的方法。我编辑了代码片段。现在起作用了。
$(function() {
// ADDING NEW INPUT FIELD
$('.button-plus').on('click', function(event) {
event.preventDefault();
let inPut = $('.input-time');
let inPutArr = jQuery.makeArray( inPut );
// console.log(inPutArr);
let lastInPut = inPutArr[inPutArr.length-1]; // get last elem of array
$(lastInPut).addClass('lastone'); // add class to it
$('.lastone').after('<input type=\"text\" class=\"input-time\">');
$('.lastone').delay(10).removeClass('lastone');
});
/* OLD CODE - NOT WORKING!!!
$('#button-calc').on('click', function(event) {
event.preventDefault();
let inPutV = $('.input-time').val();
let inPutVal = jQuery.makeArray( inPutV );
for (var i = 0; i < inPutVal.length; i++) {
var sum = sum + inPutVal[i];
}
console.log(sum);
});
*/
// NEW CODE - WORKING!!!
$('#button-calc').on('click', function(event) {
event.preventDefault();
let sum = 0;
$('.input-time').each (function () {
sum += +$(this).val();
});
console.log (sum);
});
});/* IGNORE THE STYLE ;) */
input {
width: 160px;
height: 20px;
padding: 10px 20px 10px 20px;
border: none;
border-radius: 20px;
font-size: 15px;
display: block;
margin-bottom: 10px;
}
.container {
background-color: #dde4fe;
padding: 20px;
width: 200px;
margin-right: 20px;
margin-left: auto;
margin-right: auto;
margin-top: 100px;
}
.button-plus {
width: 40px;
height: 40px;
background-color: #9aadfd;
color: #fff;
cursor: pointer;
margin-top: 10px;
margin-left: auto;
margin-right: auto;
border-radius: 20px;
display: flex;
align-items: center;
justify-content: center;
}
#button-calc {
width: 200px;
height: 40px;
background-color: #5677fc;
color: #fff;
cursor: pointer;
margin-top: 20px;
margin-bottom: 0;
}
.btn-plus {
width: 20px;
margin-left: auto;
margin-right: auto;
height: 5px;
background-color: #5677fc;
border-radius: 5px;
position: absolute;
}
.plus-1 {
transition: 0.5s;
transform: rotate(0deg);
}
.plus-2 {
transition: 0.5s;
transform: rotate(90deg);
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section class="container">
<div class="container-inner">
<input type="text" class="input-time">
<input type="text" class="input-time">
<div class="button-plus">
<div class="btn-plus plus-1"></div>
<div class="btn-plus plus-2"></div>
</div>
<input type="submit" id="button-calc" value="Calculate">
</div>
</section>
发布于 2017-10-30 17:18:45
您要做的是使用类input-time迭代所有元素,并为每个元素获取值。您不能直接获得一个值数组。
$('#button-calc').on('click', function(event) {
event.preventDefault();
let sum = 0; // Init the variable outside the loop
$('.input-time').each (function () { // This is how you iterate all .input-time element
sum += +$(this).val(); // And then you get the value
});
console.log (sum);
});$(this).val()返回输入的文本,但它是一个字符串,需要向sum添加一个数字。这就是为什么我在+之前使用$(this).val()来转换数字字符串的原因。
您还应该检查.input-time元素是否是字符串,以避免奇怪的行为。
发布于 2017-10-30 16:55:08
sum是未定义的,所以当您向它添加一个数字时,您将该数字添加到undefined中。您需要首先设置sum = 0。
帮助避免意外使用未定义值的建议,尝试在代码开始时包含'use strict' --它迫使您定义变量等。
发布于 2017-10-30 16:59:48
收集对象时不要使用val()。
inPutV = $('.input-time');
但在提取值时使用val():
sum = sum + inPutVal[i].val()
https://stackoverflow.com/questions/47020404
复制相似问题