2015-01-11 2 views
-2

나는 정확히 같은 간단한 웹 프로젝트 두 가지가 있지만 한 모든 문제가 무엇인지 말해하지 벌금과 다른 작동된다 버그Wy의 JS 방법에서는 toFixed() 반환 NaN의

내가 뭔가를 입력 나에게 코드 페이지를 내가 모두가 여기에

Here is example

작동 방법없이 "에서는 toFixed()를"그것을 시도

결과 NaN이입니다 제공

여기에3210
<!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/> 

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

    } 

} 

탄원 내가 모든 노력을하기 때문에이 문제 좀 도와 어디에 문제 모르는 JS 코드를입니다! 하나 때문에

+0

어떻게 작동하지 않나요? 문제를 설명하지 않았습니다. – JLRishe

+0

'NaN'은 어떤 입력을 생성합니까? 작동하는 것 같습니다. – meskobalazs

+0

내 프로그램의 결과가 NaN입니다 – Partizanin

답변

1

toFixed 당신에게 "NaN"을주고있다 :

  • inputValue는 숫자로 변환 할 수없는 문자열, 또는

  • #exchange1/2/3/4 인은로 변환 할 수없는 문자열 인 값을 가지고 전화 번호

... 그래서 inputValueb를 곱하면 y 교환 상자 중 하나의 값인 경우, 값은 NaN입니다. NaNtoFixed으로 전화하면 "NaN"이됩니다. 대한

것들 볼 수 있습니다 : 당신이

  • 당신은 확실히 변환하기 전에 ,을 제거해야 변환하기 전에 공백을 손질 할 필요가 아마도

    • +"1,234"이며, NaN

    • 또는 당신은있어 필드에 (기타) 숫자가 아닌 문자를 사용할 수 있습니다 (keypress은 데이터가 필드로 들어가는 유일한 방법이 아니며에서 사용하는 검사입니다.꽤 사납게 보입니다.)

  • +1

    괜찮아요, 당신의 충고가 지금 모든 일을 도왔습니다 – Partizanin

    +0

    당신은 웃을 겁니다, 문제를 발견했습니다. 서버가 그 번호를 보냈습니다. 두 번째 혼수 상태 및이 방법 때문에 toFixed()가 NaN을 생성합니다 시간이 걸렸습니다. – Partizanin