JavaScript简单";错误处理“;通过将NaN替换为条件
我想知道如何检测错误的输入类型,而不是向用户显示NaN,而是显示逻辑信息 这是我的密码:JavaScript简单";错误处理“;通过将NaN替换为条件,javascript,error-handling,nan,Javascript,Error Handling,Nan,我想知道如何检测错误的输入类型,而不是向用户显示NaN,而是显示逻辑信息 这是我的密码: <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Quadratic Root Finder</title> <script> window.onload = function() { documen
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Quadratic Root Finder</title>
<script>
window.onload = function() {
document.getElementById('calculate').onclick = function calculateQuad()
{
var inputa = document.getElementById('variablea').value;
var inputb = document.getElementById('variableb').value;
var inputc = document.getElementById('variablec').value;
root = Math.pow(inputb,2) - 4 * inputa * inputc;
root1 = (-inputb + Math.sqrt(root))/2*inputa
root2 = (-inputb + Math.sqrt(root))/2*inputa
document.getElementById('root1').value = root1;
document.getElementById('root2').value = root2;
if(root<'0')
{
document.getElementById('root1').value = 'No real solution'
document.getElementById('root2').value = 'No real solution'
}
else {
if(root=='0')
{
document.getElementById('root1').value = root1
document.getElementById('root2').value = 'No Second Answer'
}
else {
document.getElementById('root1').value = root1
document.getElementById('root2').value = root1
}
}
};
document.getElementById('erase').onclick = function()
{
document.getElementById('form1').reset();
}
}
</script>
<style>
#container
{
text-align: center;
}
</style>
</head>
<body>
<div id="container">
<h1>Quadratic Root Finder!</h1>
<form id="form1">
a:<input id="variablea" value="" type="text">
<br/>
b:<input id="variableb" value="" type="text">
<br />
c:<input id="variablec" value="" type="text">
<br />
<input id="calculate" value="Calculate!" type="button">
<input id="erase" value="Clear" type="button">
<br />
<br />
Roots:
<br />
<input id="root1" type="text" readonly>
<br />
<input id="root2" type="text" readonly>
</form>
</div>
</body>
</html>
二次寻根器
window.onload=函数(){
document.getElementById('calculate')。onclick=函数calculateQuad()
{
var inputa=document.getElementById('variablea').value;
var inputb=document.getElementById('variableb').value;
var inputc=document.getElementById('variablec')。值;
root=Math.pow(inputb,2)-4*inputa*InputUTC;
root1=(-inputb+Math.sqrt(root))/2*inputa
root2=(-inputb+Math.sqrt(root))/2*inputa
document.getElementById('root1')。value=root1;
document.getElementById('root2')。value=root2;
如果(root非常简单,下面是一个完整的示例,将漂亮的错误消息输出到;)
二次寻根器
函数getnum(id)
{//获取“n”支票号码
var输入=+(document.getElementById(id).value);
如果(isNaN(输入))
{
document.getElementById('errmsg')。innerHTML='的'+id+'值不是数字,请输入一个数字';
返回false;
}
返回输入;
}
window.onload=函数(){
document.getElementById('calculate')。onclick=函数calculateQuad()
{
var inputa=getnum('variable a');
var inputb=getnum('variable b');
var inputt=getnum('variable c');
如果(输入===false)
返回;
如果(inputb==false)
返回;
如果(inputb==false)
返回;
root=Math.pow(inputb,2)-4*inputa*InputUTC;
root1=(-inputb+Math.sqrt(root))/2*inputa;
root2=(-inputb+Math.sqrt(root))/2*inputa;
document.getElementById('root1')。value=root1;
document.getElementById('root2')。value=root2;
if(根<0)
{
document.getElementById('root1')。value='No real solution';
document.getElementById('root2')。value='No real solution';
}
否则{
如果(根==0)
{
document.getElementById('root1')。value=root1;
document.getElementById('root2')。value='没有第二个答案';
}
否则{
document.getElementById('root1')。value=root1;
document.getElementById('root2')。value=root1;
}
}
};
document.getElementById('erase')。onclick=function()
{
document.getElementById('form1').reset();
}
}
#容器
{
文本对齐:居中;
}
#嗯
{
颜色:#c00;
}
二次根查找器!
a:
b:
c:
根:
您可以在尝试在计算中使用输入之前检查输入
document.getElementById('calculate').onclick = function calculateQuad() {
var inputa = document.getElementById('variablea').value;
var inputb = document.getElementById('variableb').value;
var inputc = document.getElementById('variablec').value;
inputa = new Number(inputa); // try to convert to number
if (isNaN(inputa)) { // use built in method to check for NaN
alert('variable a is not a valid integer or whatever.');
return;
}
// TODO repeat for b and c
root = Math.pow(inputb, 2) - 4 * inputa * inputc;
root1 = (-inputb + Math.sqrt(root)) / 2 * inputa; // don't forget your semicolons.
root2 = (-inputb + Math.sqrt(root)) / 2 * inputa;
document.getElementById('root1').value = root1;
document.getElementById('root2').value = root2;
// should be comparing against integer 0, not string 0
if (root < 0) {
document.getElementById('root1').value = 'No real solution'
document.getElementById('root2').value = 'No real solution'
}
else {
if (root === 0) {
document.getElementById('root1').value = root1
document.getElementById('root2').value = 'No Second Answer'
}
else {
document.getElementById('root1').value = root1
document.getElementById('root2').value = root1
}
}
};
document.getElementById('calculate')。onclick=函数calculateQuad(){
var inputa=document.getElementById('variablea').value;
var inputb=document.getElementById('variableb').value;
var inputc=document.getElementById('variablec')。值;
inputa=新数字(inputa);//尝试转换为数字
if(isNaN(inputa)){//使用内置方法检查NaN
警报('变量a不是有效的整数或任何东西');
返回;
}
//b和c的TODO重复
root=Math.pow(inputb,2)-4*inputa*InputUTC;
root1=(-inputb+Math.sqrt(root))/2*inputa;//不要忘记分号。
root2=(-inputb+Math.sqrt(root))/2*inputa;
document.getElementById('root1')。value=root1;
document.getElementById('root2')。value=root2;
//应与整数0而不是字符串0进行比较
if(根<0){
document.getElementById('root1')。值='No real solution'
document.getElementById('root2')。值='No real solution'
}
否则{
如果(根==0){
document.getElementById('root1')。value=root1
document.getElementById('root2')。值='没有第二个答案'
}
否则{
document.getElementById('root1')。value=root1
document.getElementById('root2')。value=root1
}
}
};
我想你需要这样的东西
function NaNValueReplacement() {
for (var i = 0; i < arguments.length; i++) {
//alert(arguments[i]);
var x = (arguments[i]);
var y = document.getElementById(x).value;
var y_txtField = document.getElementById(x);
/*
alert('Argument '+i+' with name "'+x+'" ::: Value of element '+i+' is "'+y+'" and the variable type is "'+y_txtField+'"');
*/
if ( isNaN(y))
{
y_txtField.value='Some Other Value';
}
}}
函数值替换(){
for(var i=0;i
这样称呼它:
NaNValueReplacement('txt\u field\u 1'、'txt\u field\u 2'、'txt\u field\u 3'、'txt\u field\u 4');
您可以根据需要放置任意多个txt_字段
我还举了一个例子
希望能有所帮助。1进制+
可以用来转换成数字,看起来也不那么陌生;)从可用性的角度来看,永远不要使用警报。警报会很难打乱用户的工作流程,让他们感觉自己做错了什么,这只是一个好提示。
function NaNValueReplacement() {
for (var i = 0; i < arguments.length; i++) {
//alert(arguments[i]);
var x = (arguments[i]);
var y = document.getElementById(x).value;
var y_txtField = document.getElementById(x);
/*
alert('Argument '+i+' with name "'+x+'" ::: Value of element '+i+' is "'+y+'" and the variable type is "'+y_txtField+'"');
*/
if ( isNaN(y))
{
y_txtField.value='Some Other Value';
}
}}