gpt4 book ai didi

javascript - 如何添加错误处理以检查输入是否为单独函数中的数字

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

我正在寻找仅输入数字,如果还有其他输入,它将提醒您输入字母,并且必须使用其他功能。此外,它必须使用普通javascript,并且用于一个项目,并且必须具有3个正在运行的功能,这就是为什么它必须位于单独的功能中。谢谢!

<!doctype html>
<html>
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">
<link href="https://fonts.googleapis.com/css?family=Raleway" rel="stylesheet">

<meta charset="utf-8">
<title>Tip Calc.</title>
<style>
html,body{
width:100%;
height:100%;



}

body{
margin:0px;
overflow-x:hidden;
background-color: #f9f8f4 !important
}
p{
font-size: 18px !important;
font-family: 'Raleway', sans-serif;


}
h1,h2,h3{
font-family: 'Raleway', sans-serif;



}



</style>
</head>

<body>
<div class="container">
<br>
<h1 class="text-center">Tip Calculator</h1>
<br><br>
<div class="row">
<div class="col-lg-6">
<form>
<div class="form-group">
<label for="exampleInputEmail1">Total</label>
<input type="text" class="form-control" id="total" aria-describedby="emailHelp" placeholder="Enter Total Price">

</div>
<div class="form-group">
<label for="exampleInputPassword1">Tip Percentage %</label>
<input type="text" class="form-control" id="percent" placeholder="Tip Percentage">
</div>

<button type="submit" class="btn btn-primary" id="btn">Submit</button>
</form>



</div>


<div class="col-lg-6">
<h2>Total Price:</h2><h3 id="totalprice"></h3>


</div>
</div>

</div>
<script>
window.addEventListener("DOMContentLoaded",init,false);


function init(){

document.getElementById("btn").addEventListener("click", getprice, false);




}//end init function

function getprice(e) {
e.preventDefault();
math();

// var totalpriceout = document.getElementById("totalprice").value = totalValue.toFixed(2);

}
function math(){
var numVal1 = Number(document.getElementById("total").value);
var numVal2 = Number(document.getElementById("percent").value) / 100;

var totalValue = numVal1 + (numVal1 * numVal2)
document.getElementById("totalprice").innerHTML = "$" + totalValue.toFixed(2);


}






</script>
</body>
</html>

最佳答案

您要寻找的基线是parseFloat,即

var foo = "abcdef"
var bar = "123456"

foo == parseFloat(foo)
->false
bar == parseFloat(bar)
->true

因为这似乎是 homework help,所以我不想为此付出太多。

关于javascript - 如何添加错误处理以检查输入是否为单独函数中的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50031475/

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