gpt4 book ai didi

javascript - JS - typeof 不适用于输入 | document.getElementById 问题

转载 作者:行者123 更新时间:2023-11-28 17:46:34 24 4
gpt4 key购买 nike

我需要寻求帮助来解决两个问题。

  1. document.getElementById.value 保存在 let 中,无法与保存在不同 let< 中的 innerHTML 一起使用。我看到了一些类似的帖子,但它们并没有帮助我。

  2. 当我需要通过 typeof 检查数字时出现问题,它只是运行,因为 JS 没有看到它。

这是我的代码。感谢您的建议和帮助。

function wage() {
let input = document.getElementById('annualInput').value;
let output = document.getElementById('resultMonthly').innerHTML;

// it doesn't work, but it works with pure document.getElementById('result').innerHTML = input;
output = input;

/*
if (input !== '') {
// this if doesn't work even with string and number
if (typeof input == 'number')
document.getElementById('result').innerHTML = input;
} else {
document.getElementById('result').innerHTML = 'Please, type your annual wage';
}
*/

// annual wage devided by 12 months
let resultMonthly = input / 12;
let showMonthly = 'Your monthly wage: ' + resultMonthly.toFixed(2);

// monthly wage devided by 20 days
let resultDaily = resultMonthly / 20;
let showDaily = 'Your daily wage: ' + resultDaily.toFixed(2);

// daily wage devided by 8 hours
let resultHourly = resultDaily / 8;
let showHourly = 'Your hourly wage: ' + resultHourly.toFixed(2);

if (input !== '') {
document.getElementById('resultMonthly').innerHTML = showMonthly + ' $';
document.getElementById('resultDaily').innerHTML = showDaily + ' $';
document.getElementById('resultHourly').innerHTML = showHourly + ' $';
} else {
document.getElementById('resultMonthly').innerHTML = 'Please, type your annual wage';
}
}
<!DOCTYPE html>
<html>
<head>
<title>Wage App</title>
<meta charset="utf-8">
<title>Issue App</title>
<link rel="stylesheet" type="text/css" href="wageup.css">
</head>
<body>
<div class="container">
<div class="jumbotron">
<!-- the title and desc -->
<h1 class="display-4 text-center">WageApp</h1>
<p class="h6 text-center" id="paragraphTitle">
This is a simple App for calculating wage by year, month, day and hour
</p>
<hr class="my-4">

<!-- input-form -->
<form>
<div class="form-group">
<label for="annualInput">Your annual wage</label>
<input type="annualData" class="form-control" id="annualInput" aria-describedby="hep" placeholder="Annual wage in $">
<small id="help" class="form-text text-muted">We'll never share your wage with anyone else.</small>
</div>
<button onclick="wage()" type="button" class="btn btn-primary btn-sm" id="checkButton">Check</button>
</form>
</div>
<div class="alert alert-success" role="alert" id="resultMonthly"></div>
<div class="alert alert-info" role="alert" id="resultDaily"></div>
<div class="alert alert-warning" role="alert" id="resultHourly"></div>
</div>

<!-- JS -->
<script type="text/javascript" src="wageUp.js">
<!-- Jquery -->
<script src="https://code.jquery.com/jquery-3.2.1.min.js" integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4=" crossorigin="anonymous"></script>
<!-- Bootstrap -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</body>
</html>

最佳答案

问题 1:

output = input;

这不起作用,因为 output 没有引用该元素,而是存储该元素的innerHTML。

解决问题 1:

您的评论是一个很好的起点:

document.getElementById('result').innerHTML = input;

或者,您可以将元素存储到变量中,然后设置innerHTML:

var result = document.getElementById('result');
// later on
result.innerHTML = input;
<小时/>

问题 2:

if (typeof input == 'number')

这永远不会是真的,因为存储在元素的 value 属性中的值始终是字符串。

解决问题 2:

您可以通过多种方式检查输入是否是数字的字符串版本,其中一种方法是测试字符串是否不是 NaN:

if (!isNaN(input))

由于执行此检查的方法有很多,因此我会四处寻找适合您的用例的方法。

关于javascript - JS - typeof 不适用于输入 | document.getElementById 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46588585/

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