Javascript计算工具不能很好地工作

时间:2014-02-11 05:02:23

标签: javascript jquery html

我有一个Javascript计算工具,有时可以工作,有时不起作用。我不明白为什么。

以下是我的演示工具:http://propertyjungle.com.au/modern-business/tools_check.php

好吧,在我的“销售时固定代理成本”工具中有5个字段:

1)房屋销售价格
2)代理商报价率 3)其他费用

在“结果”部分有2个字段:
1)代理费:
2)减少代理人收费0.1%的费用将为您节省费用

它会自动显示结果。我的意思是按键。

所以,当我增加“房屋销售价格”和“评价报价...”时,两个结果字段都显示“NaN”。但它应该显示价值......例如:

House Sales Price  = 10000
Rated Quoted  = 0.3%
Other Fees = 0 (empty)

Result: 

Agent Fees = 30 (House Sales Prices * Rated Quoted )
Reducing.... = 10 (0.1% of House sales Price)

之后如果我增加“其他费用”,那么它会显示结果,但结果是错误的。它应该是例如:

Correct Result: 

Agent Fees = 10030 (House Sales Prices * Rated Quoted + Other Fees[increment by 10,000] )
Reducing.... = 10 (0.1% of House sales Price) 

Wrong Result :

Agent Fees = 10030 (House Sales Prices * Rated Quoted + Other Fees[increment by 10,000] )
Reducing.... = 10010.00 

以下是我的完整代码:

Javascript:

    function incrementValue()
{
    var value = parseInt(document.getElementById('pvalue1').value, 10);
    value = isNaN(value) ? 0 : value;
    value +=10000
    document.getElementById('pvalue1').value = value;
}

function decrementValue()
{
    var value = parseInt(document.getElementById('pvalue1').value, 10);
    value = isNaN(value) ? 0 : value;
    value -=10000
    document.getElementById('pvalue1').value = value;
}

function incrementValueO()
{
    var value = parseInt(document.getElementById('otherFees').value, 10);
    value = isNaN(value) ? 0 : value;
    value +=10000
    document.getElementById('otherFees').value = value;
     $('#pvalue1').trigger("change");
}

function decrementValueO()
{
    var value = parseInt(document.getElementById('otherFees').value, 10);
    value = isNaN(value) ? 0 : value;
    value -=10000
    document.getElementById('otherFees').value = value;
}


function toggleIncrement()
{
    var value = parseFloat(document.getElementById('pvalue2').value, 10);
    value = isNaN(value) ? 0 : value;
    value +=0.1
    value = parseFloat(value).toFixed(2)
    document.getElementById('pvalue2').value = value;
     $('#pvalue1').trigger("change");
}

function toggleDecrement()
{
    var value = parseFloat(document.getElementById('pvalue2').value, 10);
    value = isNaN(value) ? 0 : value;
    value -=0.1
    value = parseFloat(value).toFixed(2)
    document.getElementById('pvalue2').value = value;
     $('#pvalue1').trigger("change");
}

jQuery(document).ready(function(){
 $('#pvalue1').change(function(){
 var agentfee = parseFloat($('#pvalue1').val(), 10) * parseFloat($('#pvalue2').val(), 10) / 100; 
 console.debug( parseFloat(document.getElementById('otherFees').value));
 var otherFees = parseFloat(document.getElementById('otherFees').value);
 var totalOtherFees = agentfee + otherFees;
 $('#pvalue3').val(totalOtherFees);

 var percentagereduce = parseFloat($('#pvalue2').val(), 10) - 0.1;
 var newvalue = parseFloat($('#pvalue1').val(), 10) * percentagereduce / 100; 
 $('#pvalue4').val(newvalue);         
 var takevalue1 = parseFloat($('#pvalue3').val(), 10);
 var takevalue2 = parseFloat($('#pvalue4').val(), 10);
 var finalvalue = takevalue1 - takevalue2;
 var finalvalue = parseFloat(finalvalue).toFixed(2)
 $('#pvalue5').val(finalvalue);
});

  $('#pvalue2').change(function(){


 var agentfee = parseFloat($('#pvalue1').val(), 10) * parseFloat($('#pvalue2').val(), 10) / 100; 
 $('#pvalue3').val(agentfee);

 var percentagereduce = parseFloat($('#pvalue2').val(), 10) - 0.1;
 var newvalue = parseFloat($('#pvalue1').val(), 10) * percentagereduce / 100; 
 $('#pvalue4').val(newvalue);         
 var takevalue1 = parseFloat($('#pvalue3').val(), 10);
 var takevalue2 = parseFloat($('#pvalue4').val(), 10);
 var finalvalue = takevalue1 - takevalue2;

 $('#pvalue5').val(finalvalue);    
});   
   $('#otherFees').change(function(){
 var agentfee = parseFloat($('#pvalue1').val(), 10) * parseFloat($('#pvalue2').val(), 10) / 100; 
 console.debug( parseFloat(document.getElementById('otherFees').value));
 var otherFees = parseFloat(document.getElementById('otherFees').value);
 var totalOtherFees = agentfee + otherFees;
 $('#pvalue3').val(totalOtherFees);

 var percentagereduce = parseFloat($('#pvalue2').val(), 10) - 0.1;
 var newvalue = parseFloat($('#pvalue1').val(), 10) * percentagereduce / 100; 
 $('#pvalue4').val(newvalue);         
 var takevalue1 = parseFloat($('#pvalue3').val(), 10);
 var takevalue2 = parseFloat($('#pvalue4').val(), 10);
 var finalvalue = takevalue1 - takevalue2;
 var finalvalue = parseFloat(finalvalue).toFixed(2)
 $('#pvalue5').val(finalvalue);   
});
});   

Html代码:

    <table border="0" cellpadding="5">
<tr>
    <td>House Sale Price:</td>
    <td>$</td>
    <td><input name="pvalue1" class="form-control"  onkeypress="validate(event)" placeholder=" Enter Sale Price" type="number" value="<?=$pvalue1?>" id="pvalue1" size="20" required ></td>
    <td><input type="button" onClick="incrementValue()" value="+" /><input type="button" onClick="decrementValue()" value="-" /> </td>
</tr>
<tr>
    <td>Rate quoted by agent:</td>
    <td>%</td>
    <td><input name="pvalue2"  class="form-control" onkeypress="validate(event)" placeholder=" Percentage" type="number" value="<?=$pvalue2?>" id="pvalue2" size="20"  required ></td>
    <td><input type="button" onClick="toggleIncrement()" value="+" /><input type="button" onClick="toggleDecrement()" value="-" /></td>
</tr>
<tr>
    <td>Other Fees</td>
    <td>$</td>
    <td><input name="otherFees" class="form-control"  onkeypress="validate(event)" placeholder=" Enter Sale Price" type="number" value="<?=$pvalue1?>" id="otherFees" size="20" required ></td>
    <td><input type="button" onClick="incrementValueO()" value="+" /><input type="button" onClick="decrementValueO()" value="-" /> </td>
</tr>

</table>                                                                                                                                                                                                                   
 <input name="doRegister" type="submit" id="doRegister" value="Calculate" style="color:white;font-size:20px;" class="btn btn-primary">

<br><br>
<h2>Results</h2>
<table>
<tr>
<td>Agent Fees:</td>
<td>$</td>
<td><input name="pvalue3"  onkeypress="validate(event)"  class="form-control"  placeholder="" type="number" value="<?=$pvalue3?>" id="pvalue3" size="10" class="resultfield"  ></td></tr>
<tr>
<td></td>
<td><span id='show-me'  class="form-control" style='display:none'><input name="pvalue4" placeholder="" type="number" value="<?=$pvalue4?>" id="pvalue4" size="10" class="resultfield"  ></span></td></tr>
<tr>
<td>Reducing the rate the agent is charging by 0.1% will save you: </td>
<td>$</td>
<td><input name="pvalue5"   onkeypress="validate(event)" class="form-control"  placeholder="" type="number" value="<?=$pvalue5?>" id="pvalue5" size="10" class="resultfield"  ></td>
</tr>
</table>  
很多,谢谢你。

1 个答案:

答案 0 :(得分:0)

var agentfee = parseFloat($('#pvalue1').val(), 10) * parseFloat($('#pvalue2').val(), 10) / 100; 
console.log($('#pvalue1').val());  //3
console.log( $('#pvalue2').val());  // ""

parseInt("")是NaN

你在上面的代码中处理isNaN,在这里实现相同的逻辑。