Javascript添加不起作用
Javascript addition not working out
我只想为我的小项目将英尺转换为英寸,但它似乎不起作用。它所做的只是将最后一个数字添加到第一个数字的末尾。我一定是做错了什么。我只想将第一个数字相乘,然后在该数字上添加英寸。谢谢
<!doctype>
<html>
<head>
<meta charset="utf-8">
<title>Are you tall enough</title>
<script type="text/javascript">
function ride(){
var rideMin = 64;
var ht = document.getElementById("height").value;
if( ht >= 64){
alert("You may go on the ride!!");
}else{
alert("Sorry, you may not go on the ride");
}
}
function converter(){
var aa = document.getElementById("feet").value;
var bb = document.getElementById("inches").value;
var cc = (aa * 12);
var resu = bb+cc;
document.getElementById("results").value = resu;
}
</script>
<style>
.container{
width: 960px;
}
input{
width:250px;
height: 150px;
margin-left: 375px;
}
img{
margin-left: 300px;
max-width: 100%;
width: 40%;
margin-bottom: 100px;
}
h1{
margin-left: 200px;
margin-bottom: 50px;
color: blue;
}
.conversion{
margin: 0;
width: 100px;
height: 50px;
}
.convert{
margin-left: 390px;
}
.results{
width: 100px;
height: 50px;
margin-left: 438px;
margin-bottom: 50px;
}
</style>
</head>
<body>
<div class="container">
<h1> Are you tall enough to ride the roller coaster</h1>
<img src="roller.png">
<div class="convert">
<h3>Convert feet to inches</h3>
<input class="conversion" type="text" id="feet" placeholder="Enter feet here">
<input class="conversion" type="text" id="inches" placeholder="Enter inches here">
<input class="conversion" type="submit" onclick="converter()">
</div>
<div class="result">
<input id="results" type="text" readonly>
<div class="height">
<input type="text" id="height" placeholder="Please enter your height in inches please">
<input type="submit" value="sumbit" onclick=ride()>
</div>
</div>
</body>
</html>
请改用这个:
function ride(){
var rideMin = 64;
var ht = document.getElementById("height").value;
if( parseInt(ht) >= 64){
alert("You may go on the ride!!");
}else{
alert("Sorry, you may not go on the ride");
}
}
function converter(){
var aa = parseFloat(document.getElementById("feet").value);
var bb = parseFloat(document.getElementById("inches").value);
var cc = (aa * 12);
var resu = bb+cc;
document.getElementById("results").value = resu;
}
如果你不想使用 parseInt
,你可以使用加号将字符串强制为整数。
var aa = +document.getElementById("feet").value;
var bb = +document.getElementById("inches").value;
小提琴。
使用 parseInt 转换为整数:
function converter(){
var aa = parseInt(document.getElementById("feet").value, 10);
var bb = parseInt(document.getElementById("inches").value, 10);
var cc = (aa * 12);
var resu = bb+cc;
document.getElementById("results").value = resu;
}
相关文章:
- Meteor-添加用户自定义字段的方法不起作用
- JS动态添加字段-删除按钮不起作用
- 添加/删除类淡入淡出不起作用
- 在Internet Explorer中,向所选内容添加选项不起作用
- 添加事件侦听器不起作用
- .hasClass 在 onclick 函数中不起作用,但删除和添加类函数可以
- 通过php添加到变量won'不起作用
- angular元素在通过ng类添加类时不起作用
- 我尝试将引导下拉按钮添加到 但下拉菜单不起作用.我该怎么做
- 在 Javascript 不起作用的情况下为数字添加逗号
- 由 JavaScript 控制的 CSS3 单向转换在按顺序添加/删除类时不起作用
- 在我的管理面板上添加更多不起作用的按钮
- 在 ajax 中添加跨源标头,不起作用
- 添加/删除类在 Chrome 中对我不起作用
- 具体化选择-不'动态添加选项时不起作用
- 在Javascript中动态添加列时,单击事件不起作用
- 双击已添加的选择选项不起作用
- 在每个细胞剂量上添加字形图标'不起作用
- 通过按钮将不同的文本添加到文本区域-不起作用
- JavaScript/jQuery-脚本不起作用-添加&删除类,合并数组-测试用例