如何解决这个计算脚本,具有"南"在成本中? [英] How to fix this calculate script which has "NaN" in cost box?

查看:120
本文介绍了如何解决这个计算脚本,具有"南"在成本中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对成本计算剧本,但我有一个小问题,当我尝试空的价格中,有李楠在成本中。

我不知道问题出在哪里。我做了以下变化: digitsVal = isNaN(digitsVal)?0:digitsVal; 成为 VAR digitsVal = isNaN(digitsVal)?0:digitsVal; 和第一本code是工作,但第二次我试图在成本框中空,还有李楠在成本中。

如何解决这个计算的脚本?有没有这个剧本的解决方案?

例如:

<!DOCTYPE HTML> < HTML> < HEAD> <元字符集=utf-8> <冠军> berkelilingkesemua.info< /标题> &所述;脚本的src =htt​​ps://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js>&所述; /脚本> < /头> <身体GT; <脚本> $(文件)。就绪(函数(){   VAR价格= $('#价),       手机= $('#PHONE_NUMBER),       位数= $('#的数字'),       总和= $('#总和');   功能calculateSum(){     VAR digitsVal = parseInt函数(phone.val()SUBSTR(-2));     digitsVal = isNaN(digitsVal)0:digitsVal;     digits.val(digitsVal);     sum.val(parseFloat(price.val())+ digitsVal);   };   phone.on('的keyup',calculateSum);   price.on('的keyup',calculateSum); }); < / SCRIPT>                  <表格的宽度=300px的边界=1>     &其中; TR>         &所述; TD宽度=40PX→1&其中; / TD>         < TD>价格< / TD>         < TD><输入类=TXTNAME =价格型=文本ID =价格>< / TD>     < / TR>     &其中; TR>         &其中; TD→2&其中; / TD>         < TD>电话号码和LT; / TD>         < TD><输入名称=PHONE_NUMBER类型=文本ID =PHONE_NUMBER>< / TD>     < / TR>     &其中; TR>         &其中; TD→3&所述; / TD>         &其中; TD→2位数&所述; / TD>         < TD><输入类=TXTNAME =数字类型=文本ID =数字只读>< / TD>     < / TR>     &所述; TR的id =求和>         < TD>&安培; NBSP;< / TD>         < TD ALIGN =权利与GT;成本:其中; / TD>         < TD ALIGN =中心><输入类型=文本名称=求和ID =合计值=0>< / SPAN>< / TD>     < / TR> < /表> < /身体GT; < / HTML>

解决方案

替换:

  sum.val(parseFloat(price.val())+ digitsVal);
 

使用:

  isNaN(parseFloat(price.val())+ digitsVal)? sum.val(0):sum.val(parseFloat(price.val())+ digitsVal);
 

我觉得这应该为你工作。

I have a calculate script for cost, but I have a little problem when I try to empty price box, there is "NaN" in cost box.

I do not know where is the problem. I have made following changes: digitsVal=isNaN(digitsVal)?0:digitsVal; became var digitsVal=isNaN(digitsVal)?0:digitsVal; and first this code is working, but second I try to empty in cost box, there is still "NaN" in cost box.

How to fix this calculate script ?. Are there solution about this script?

Example :

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>berkelilingkesemua.info</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>

<body>

<script>
$(document).ready(function() {
  var price=$('#price'),
      phone=$('#phone_number'),
      digits=$('#digits'),
      sum=$('#sum');
  function calculateSum() {
    var digitsVal=parseInt(phone.val().substr(-2));
    digitsVal=isNaN(digitsVal)?0:digitsVal;
    digits.val(digitsVal);
    sum.val(parseFloat(price.val())+digitsVal);
  };
  phone.on('keyup', calculateSum);
  price.on('keyup', calculateSum);
});
</script>
        
        <table width="300px" border="1">
    <tr>
        <td width="40px">1</td>
        <td>Price</td>
        <td><input class="txt" name="price" type="text" id="price"></td>
    </tr>
    <tr>
        <td>2</td>
        <td>Phone Number</td>
        <td><input name="phone_number" type="text" id="phone_number"></td>
    </tr>
    <tr>
        <td>3</td>
        <td>2 Digits</td>
        <td><input class="txt" name="digits" type="text" id="digits" readonly></td>
    </tr>
    <tr id="summation">
        <td>&nbsp;</td>
        <td align="right">Cost :</td>
        <td align="center"><input type="text" name="sum" id="sum" value="0"></span></td>
    </tr>
</table>

</body>
</html>

解决方案

Replace:

sum.val(parseFloat(price.val())+digitsVal);

With:

isNaN(parseFloat(price.val())+digitsVal) ? sum.val(0) : sum.val(parseFloat(price.val())+digitsVal);

I think this should work for you

这篇关于如何解决这个计算脚本,具有&QUOT;南&QUOT;在成本中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆