gpt4 book ai didi

javascript - 表单中表格中的功能按钮

转载 作者:行者123 更新时间:2023-12-03 08:24:33 24 4
gpt4 key购买 nike

我正在构建一个网站并且已经完成,但是代码中有一个相当大的错误。我创建了一个表,其中有一个按钮,该按钮是计算某些表达式并将计算结果显示在表中的函数。计算发生但不显示在表中。这是我所拥有的:

<!DOCTYPE html>
<html>
<!--DB 11/2/2015-->
<!-- W3 Schools was referenced in building this code-->
<head>
<meta charset="utf-8">
<title>Assignment 8</title>
<link href="images/avatar.png" rel="shortcut icon" type="image/png">
<link href="css/stylesheet.css" rel="stylesheet" type="text/css">
<script src="js/javascript.js" type="text/javascript"></script>
<style type="text/css">
</style>

</head>
<body onload="fillitin()">
<header>
</header>
<aside>
</aside>
<div id="main">


<h1> Assignment 8: Operators and Expression</h1>
<br>
<p id="demo"></p>
<script>


var d = new Date();
months = ['Janurary','Feburary','March','April','May','June','July','August','September','October','November','December']
var date = months[d.getMonth()]+" "+d.getDate()+" "+d.getFullYear();


document.getElementById("demo").innerHTML = date;
</script>



<h2 style="text-align:center"> S'mores!</h2>

<h4> CLick on the button serves to learn the amount of ingredients needed to make S'mores!</h4>
<table style="width:50%" border="1">
<form name="myform" method="">
<tr>
<td> <br>
<input type="text" id="num1" value="1">
</td>
<td>
<button onclick="myFunction()">Serves</button>
</td>

</tr>
</form>
<tr>
<td id="M"></td>
<td>Large Marshmallows</td>
</tr>
<tr>
<td id="G"></td>
<td>Graham Cracker</td>
</tr>
<tr>
<td id="C"></td>
<td>Ounches Chocolate</td>
</tr>

</table>
<script>
function fillitin() {
document.getElementById("M").InnerHTML="1";
document.getElementById("G").InnerHTML="1";
document.getElementById("C").InnerHTML="1.5";
}

function myFunction() {
var x=document.getElementById("num1").value;
document.getElementById("M").InnerHTML=x;
document.getElementById("G").InnerHTML=x;
document.getElementById("C").InnerHTML=x*1.5;


}
</script>

<ul>
<li>
Heat the marshmallow over an open flame until it begins to brown and melt.
</li>
<li>Break the graham cracker in half. Put the chocolate on one half of the cracker, and put the warm marshmallow on the other. </li>
<li>Enjoy! Don’t burn your mouth!</li>
</ul>





<img alt="picture of smores" height="129" src="images/picture1.jpg" width="194">
<cite> picture taking from <a href="http://www.instructables.com/id/no-campfire-smores/"> http://www.instructables.com/id/no-campfire-smores/
</a></cite>
</div>
<footer>
</footer>
</body>
</html>

最佳答案

您使用的 InnerHTML (大写 I)是错误的。正确的函数是 .innerHTML

<小时/>

此外,如果您不希望按钮发布表单,则应该添加它 type="button"

更多详细信息:Can I make a button not submit a form

关于javascript - 表单中表格中的功能按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33612938/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com