gpt4 book ai didi

JavaScript 通过单击按钮执行简单计算并将结果放入文本框中

转载 作者:行者123 更新时间:2023-12-01 02:26:25 24 4
gpt4 key购买 nike

我试图在单击按钮时激活一些 JavaScript。

该操作用于在文本框中显示简单的计算。

这是我到目前为止所得到的,但目前不起作用:

function to_mjd() {

var number;

number = 5 + 7;

document.getElementById("mjd").value = number;

}
<input id="mjd" class="textbox2" type="text" name="mjd" placeholder="NNNNN" maxlength="5">

<input type="submit" onclick="to_mjd();" value="CONVERT">

谁能指出我错在哪里吗?

<小时/>

我正在编辑我的原始帖子以添加完整的 html 和 javascript 代码。希望这会有所帮助。

整个 HTML:

<!DOCTYPE html>

<html>

<head>
<title>Converter</title>
<link href="https://fonts.googleapis.com/css?family=Quicksand" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="style.css">
<!-- <link rel="shortcut icon" href="favicon.ico" > -->
</head>

<body>





<div class="band1">
<center>
<h1>CONVERTER</h1>
</center>
</div>

<div id ="band" class="band2">
<center>
<form>
<span class="span1">
<span class="date">
Year:<br><br>
<input class="textbox1" type="text" name="year" placeholder="yyyy" maxlength="4">
</span>
<span class="date">
Month:<br><br>
<input class="textbox1" type="text" name="month" placeholder="mm" maxlength="2">
</span>
<span class="date">
Day:<br><br>
<input class="textbox1" type="text" name="day" placeholder="dd" maxlength="2">
</span>
</span>
<span class="span2">
<input type="button" onclick="to_mjd();" value="CONVERT">
</span>
<span class="span3">
MJD:<br><br>
<input id="mjd" class="textbox2" type="text" name="mjd" placeholder="NNNNN" maxlength="5">
</span>
</form>
</center>
</div>

<script src="https://rawgit.com/bgrins/TinyColor/master/tinycolor.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script src="scripts.js"></script>
</body>

</html>

整个 JavaScript:

$(document).ready(function(){

var color = '#'+('000000' + (Math.random()*0xFFFFFF<<0).toString(16)).slice(-6);

document.getElementById("band").style.background = color;

var c = tinycolor(color);

if (c.isDark())
document.getElementById("band").style.color = "#FFFFFF";
else
document.getElementById("band").style.color = "#000000";

function to_mjd() {

var number;

number = 5+7;

document.getElementById("mjd").value = number;

}

});

最佳答案

to_mjd 是本地函数,按钮看不到它。只需更改 js 文件中的函数位置即可:

$(document).ready(function(){

var color = '#'+('000000' + (Math.random()*0xFFFFFF<<0).toString(16)).slice(-6);

document.getElementById("band").style.background = color;

var c = tinycolor(color);

if (c.isDark())
document.getElementById("band").style.color = "#FFFFFF";
else
document.getElementById("band").style.color = "#000000";

});


var to_mjd = function() {

var number;

number = 5+7;

document.getElementById("mjd").value = number;

}

关于JavaScript 通过单击按钮执行简单计算并将结果放入文本框中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48760777/

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