javascript - Wy js method toFixed() return NaN - Stack Overflow

I have two exactly the same simple web project,but one everything is working fine and the other not, te

I have two exactly the same simple web project,but one everything is working fine and the other not, tell me what the problem is, where a bug

when I enter something gives me the result NaN

I tried to do it without the method "toFixed()"all works

Here is example

Here is the code page

 <!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/>

Here is js code

 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);

    }

}

Pleas help me with this problem,because i tried everything and do not know where the problem!

I have two exactly the same simple web project,but one everything is working fine and the other not, tell me what the problem is, where a bug

when I enter something gives me the result NaN

I tried to do it without the method "toFixed()"all works

Here is example

Here is the code page

 <!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/>

Here is js code

 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);

    }

}

Pleas help me with this problem,because i tried everything and do not know where the problem!

Share Improve this question edited Jan 11, 2015 at 14:46 Partizanin asked Jan 11, 2015 at 14:33 PartizaninPartizanin 3332 gold badges5 silver badges12 bronze badges 3
  • What about it is not working? You haven't described the problem. – JLRishe Commented Jan 11, 2015 at 14:36
  • For what input does it produce NaN? It seems to work. – meskobalazs Commented Jan 11, 2015 at 14:37
  • Result of count of my program is NaN – Partizanin Commented Jan 11, 2015 at 14:52
Add a ment  | 

1 Answer 1

Reset to default 5

toFixed is giving you "NaN" because either:

  • inputValue is a string that cannot be converted to a number, or

  • #exchange1/2/3/4 has a value which is a string that cannot be converted to a number

...and so when you multiply inputValue by the value of one of those exchange boxes, you're getting the value NaN. When you call toFixed on NaN you get "NaN".

Things to look for:

  • Perhaps you need to trim whitespace before converting

  • You certainly need to remove , before converting, +"1,234" is NaN

  • Or you're allowing (other) non-numeric characters into fields (keypress is not the only way data gets into fields, and the check used by your isNumberKey looks pretty dodgy)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745248362a4618524.html

相关推荐

  • javascript - Wy js method toFixed() return NaN - Stack Overflow

    I have two exactly the same simple web project,but one everything is working fine and the other not, te

    8小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信