我如何 return JavaScript 中函数的值到 HTML 中的文本框

How do I return a value from a function in JavaScript to a textbox in HTML

这是我的 HTML 代码:

<head>

</head>

<body>

   LIMIT<input id='limit' name='' value='' class=''>

   <button id='go' class=''>GO</button>

   TOTAL<input id='total' name='' value='' class=''>

   <script src='js/limitfor.js'></script>


</body>

这是我的 JavaScript:

document.getElementById('go').onclick = function () {

 var limit = document.getElementById('limit').value;

 limit = parseFloat(limit);

 total = 0;

 for (i=0; i<=limit ;i++) {

     total = total + i;        

 };

};

如果我提醒总数,我可以看到该功能有效,但我需要将总数显示在文本框中而不是弹出警报中。

您需要设置输入元素的value

document.getElementById("total").value = total;

for 循环完成后,只需在总计文本框中赋值即可

 var limit = document.getElementById('limit').value;

 limit = parseFloat(limit);

 total = 0;

 for (i=0; i<=limit ;i++) {

   total = total + i;        

};
document.getElementById("total").value = total; 

};

首先 select 表单中的特定元素(即总文本字段)并使用赋值运算符“=”设置其值

document.getElementById("total").value=total;

使用document.getElementById(put id of the text area where you want to output your answer or result).value = answer(whatever is your answer or result you want to reflect in textbox or textarea)