[英]Datalist not into text box when I'm trying to add my array's values to it as options
[英]I'm trying to make text box in javascript, and to send the entered values in a textbox in a array to add them
在javascript中创建的文本框,需要将这些值传递给数组以添加它们吗? 我怎么做 ? 我只希望我的代码简短
我被卡住了! 码:
<html>
<head>
<title> 2 NUM ADD VIA ARRAY </title>
</head>
<body bgcolor= "red" </body>
<script type = "text/javascript">
function sum()
{
var sum = [],i=0;
sum[i]= document.getElementById('i+1').value;
alert("sum =" + sum);
}
</script>
<table border=2>
<TR>
<TD>
<input type="text" id="1" /input>
</TD>
<TD>
<input type="text" id="2" /input>
</TD>
</TR>
</table>
<button name="btHello" onclick="sum();">sum</button>
</html>
根据我的说法,在这种情况下不需要使用Array。我建议您即使不使用数组也要这样做。 这使您的代码简单而简短
<html>
<head>
<title> 2 NUM ADD VIA ARRAY </title>
</head>
<body bgcolor= "red" </body>
<script type = "text/javascript">
function sum()
{
var sum= 0;
$('.textbox').each(function() {
sum += Number($(this).val());
});
alert(sum);
}
</script>
<form>
<table border=2>
<TR>
<TD>
Text 1 :<input type="text" class="textbox" id="1" />
</TD>
<TD>
Text 2 :<input type="text" class="textbox" id="2" />
</TD>
</TR>
</table>
Submit : <input type = "submit" name="btHello" onclick="sum();">
</form>
</html>
希望我的例子对您有所帮助。 我建议使用jQuery。 它使它变得更加容易。
function foo() { var s = []; $(".foo").each(function() { s.push( $(this).val() ); }); s = s.join(" "); alert("sum = " + s); } $("#btHello").on("click", function() { foo(); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <table border=2> <tr> <td> <input type="text" class="foo" /input> </td> <td> <input type="text" class="foo" /input> </td> </tr> </table> <button id="btHello">sum</button>
我完全确定您想要什么结果,但这将对您有所帮助。 如果您追求的目标不尽相同,请尝试多解释一下您的目标。 :)
快乐鳕鱼
function sum() { var sum = []; sum.push(document.getElementById('1').value); sum.push(document.getElementById('2').value); // sum of all values totalSum = 0; for (var i = 0; i < sum.length; i++) { totalSum += parseInt(sum[i]); } alert("sum = " + totalSum); }
<html> <head> <title> 2 NUM ADD VIA ARRAY </title> </head> <body bgcolor= "red" </body> <table border=2> <TR> <TD> <input type="text" id="1" /input> </TD> <TD> <input type="text" id="2" /input> </TD> </TR> </table> <button name="btHello" onclick="sum();">sum</button> </html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.