简体   繁体   English

如何将值从 JavaScript 中的函数返回到 HTML 中的文本框

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

This is my HTML code:这是我的 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>

And this is my JavaScript:这是我的 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;        

 };

};

If I alert the total, I can see that the function works, but I need the total to be in the textbox rather than in a pop up alert.如果我提醒总数,我可以看到该功能有效,但我需要总数在文本框中而不是在弹出警报中。

您将需要设置输入元素的value

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

首先选择表单中的特定元素(即总文本字段),然后使用赋值运算符'='设置其值

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

Just assign the value in total text box after your for loop is completed 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; 

};

use 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)使用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)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM