简体   繁体   English

javascript onclick功能不起作用

[英]javascript onclick function is not working

i tried this code in vs 2017 but it is not working. 我在vs 2017中尝试了此代码,但无法正常工作。 i cant console the total and other text variables but i am unable to populate the results in the text box. 我无法控制总变量和其他文本变量,但无法在文本框中填充结果。

 function myFunction1() { var text1 = document.getElementById("txt1").value; console.log(text1) var text2 = document.getElementById("txt2").value; var text3 = document.getElementById("txt3").value; var text4 = document.getElementById("txt4").value; var total1 = parseFloat(text1) + parseFloat(text2) + parseFloat(text3) + parseFloat(text4); console.log(total1) document.getElementById("txt1").innerHTML = total1; } 
 <input type="text" id="txt1" /> <input type="text" id="txt2" /> <input type="text" id="txt3" /> <input type="text" id="txt4" /> <input type="text" id="total" /> <input type="submit" onclick="myFunction1()" /> 

Use value instead of innerHTML on your result input. 在结果输入中使用value而不是innerHTML Because input elements doesn't have innerHTML 因为输入元素没有innerHTML

document.getElementById("monthlyAmount").value = total1

innerHTML means HTML between starting and ending tag of element.As <input> elements doesn't have closing tag so it has no innerHTML innerHTML表示元素的开始和结束标记之间的HTML。 <input>元素没有结束标记,因此没有innerHTML

您应该使用value来设置输入中的文本:

document.getElementById("monthlyAmount").value = total1;

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

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