Wy js 方法 toFixed() 返回 NaN

Wy js method toFixed() return NaN

本文关键字:返回 NaN toFixed js 方法 Wy      更新时间:2023-09-26

我有两个完全相同的简单 Web 项目,但一个一切正常,另一个则不然,告诉我问题是什么,错误在哪里

当我输入某些东西时给我结果 NaN

我试图在没有方法的情况下做到这一点 "toFixed()"所有作品

这是示例

这是代码页

 <!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Конвертер валют</title>
     <script src="jquery-2.1.1.min.js"></script>
    <script src="page.js"></script>
    <link type="text/css" rel="stylesheet" href="style.css"/>
</head>
<body>
<div class="Converter">
    <hr>
   input value  <label for="inputValue"></label><input type="text" value="0.00" onkeypress="return isNumberKey(event)"
                                                    onkeyup="count(this.value)"
                                                    onfocus="if (this.value == '0.00') this.value='';"
                                                    onblur="if (this.value == '') {this.value = '0.00'; }"
                                                    id="inputValue">
    <p>Result</p>
    <ul>
        <li><input type="text" readonly value="0.00" 
                   onblur="if (this.value == '') {this.value = '0.00'; }" id="conventUSD">
            <input type="text" id="value1" readonly size="1" value="USD">
            <input value= "5.553" readonly id="exchange1"></li>
        <li><input type="text" readonly value="0.00" 
                   onblur="if (this.value == '') {this.value = '0.00'; }" id="conventRUB">
            <input type="text" id="value2" readonly size="1" value="RUB">
            <input value= "10.553" readonly id="exchange2"></li>
        <li><input type="text" readonly value="0.00" 
                   onblur="if (this.value == '') {this.value = '0.00'; }" id="conventEUR">
            <input type="text" id="value3" readonly size="1" value="EUR">
            <input value= "15.553" readonly id="exchange3"></li>
        <li><input type="text" readonly value="0.00" 
                   onblur="if (this.value == '') {this.value = '0.00'; }" id="conventUAH">
            <input type="text" id="value4" readonly size="1" value="PLN">
            <input  value= "20.553" readonly id="exchange4"></li>
    </ul>
</div>
            <body/>
<html/>

这是 js 代码

 function isNumberKey(evt) {
    var charCode = ((evt.which) ? evt.which : event.keyCode);
    return !(charCode > 31 && (charCode != 46 && charCode != 44 && (charCode < 48 || charCode > 57)));
}

function count(inputValue) {
    if (inputValue != "" && inputValue != "0.00") {
        $('#conventUSD').val(($("#exchange1").val() * inputValue).toFixed(4));
        $('#conventRUB').val(($("#exchange2").val() * inputValue).toFixed(4));
        $('#conventEUR').val(($("#exchange3").val() * inputValue).toFixed(4));
        $('#conventUAH').val(($("#exchange4").val() * inputValue).toFixed(4));
    } else {
        var defaultValue = "0.00";
        $('#conventUSD').val(defaultValue);
        $('#conventEUR').val(defaultValue);
        $('#conventRUB').val(defaultValue);
        $('#conventUAH').val(defaultValue);
    }
}

请帮助我解决这个问题,因为我尝试了一切,但不知道问题出在哪里!

toFixed给你"NaN",因为:

  • inputValue是无法转换为数字的字符串,或

  • #exchange1/2/3/4有一个值,该值是无法转换为数字的字符串

。因此,当您将inputValue乘以其中一个交换框的值时,您将获得NaN的值。当您拨打toFixed NaN时,您会"NaN".

要寻找的东西:

  • 也许您需要在转换前修剪空格

  • 您当然需要在转换之前删除,+"1,234" NaN

  • 或者你允许(其他)非数字字符进入字段(keypress不是数据进入字段的唯一方式,而且你的isNumberKey使用的检查看起来很狡猾)