【问题标题】:jquery alert returns NaNjquery 警报返回 NaN
【发布时间】:2017-01-02 07:43:24
【问题描述】:

我是新来的,也是 JQuery 的新手,我卡住了

HTML:

<div class='col-md-2'>$pro_name</div>
<div class='col-md-2'><input type='text' class='form-control qty' pid='$pro_id' id='$qty-$pro_id' value='$qty' ></div>
<div class='col-md-2'><input type='text' class='form-control price' pid='$pro_id' id='$price-$pro_id' value='$pro_price' disabled></div>
<div class='col-md-2'><input type='text' class='form-control total' pid='$pro_id' id='$total-$pro_id' value='$total' disabled></div>

查询:

$("body").delegate(".qty","keyup", function(){   
    var pid = $(this).attr("pid");          
    var qty = $("#qty-"+pid).val();
    var price = $("#price-"+pid).val();
    var total = qty * price;        
    alert(total);
})

Jquery 警报返回 NaN。

【问题讨论】:

  • 您检查过 pid、qty 和 price 的值吗?
  • 你想做什么?在 htm 字段中不需要 ut $
  • 我正在做这个视频中的练习项目:youtube.com/…。我的问题从 6:45 开始。我没有得到全面警报

标签: jquery alert nan


【解决方案1】:

您的total 值不是数字,它是一个字符串,您尝试将两个字符串组合起来以得到nan。将您的值更改为一些数字数据,然后尝试。

【讨论】:

    【解决方案2】:

    使用下面的代码。您的代码中没有错误。

    1. 无需在 html 字段中使用“$”。
    2. 您已禁用价格字段,您需要在其中输入值。

    更多关于 html Id 属性的使用方法,请查看此 SO link

    <div class='col-md-2'>
        <input type='text' class='form-control qty' pid='pro_id' id='qty_pro_id'  ></div>
        <div class='col-md-2'><input type='text' class='form-control price' pid='pro_id' id='price_pro_id' ></div>
        <div class='col-md-2'><input type='text' class='form-control total' pid='pro_id' id='total_pro_id' disabled>
    </div>
    
    $("body").delegate(".qty,.price","keyup", function(){   
    
      var pid = $(this).attr("pid");              
      var qty = $("#qty_"+pid).val();
      var price = $("#price_"+pid).val();
      var total = qty * price;        
      alert(total);
    })
    

    JS Fiddle

    【讨论】:

    • 此代码没有帮助。也许我没有提供所有信息。 youtube.com/watch?v=XVivL6cupxo 我的问题从 6:45 开始。我没有得到全面警报
    • 这里缺少什么?
    • qty 将是客户增加产品数量的输入字段,价格已经在数据库中。 Total 必须将这两个值相乘,并且在 disabled 字段中需要立即更改总价。在这种情况下,Jquery 只是检查是否一切正常,但我得到 NaN 检查这个小提琴 jsfiddle.net/ufqsmLwy @VedranBrendy - Nalin Aggarwal - 这不是我需要的
    【解决方案3】:

    您最初遇到的一个问题是您的选择器语法。如果你想在 jQuery 的选择器中使用特殊字符,也就是 '$',你必须使用双斜杠 '\\' 转义它们,就像我在下面的示例中所做的那样。

    您当前拥有的值显然不是数字,但即使是数字,您仍然会收到“NAN”。您必须在值参数上使用 parseInt(),因为值本身被视为字符串,而不是数字。

    parseInt() 接受两个参数。第一个是您要转换为整数的值,第二个是基数。在我的示例中,我使用了一个基数“10”,它将值转换为一个简单的十进制数。

    如果您想了解有关 parseInt 的更多信息,请点击以下链接:http://www.w3schools.com/jsref/jsref_parseInt.asp

    我继续将您的两个输入变量的默认值设置为实际数字,以便您自己测试。

    $(document).ready(function() {
      $("body").delegate(".qty","keyup", function(){   
        var pid = $(this).attr("pid");          
        var qty = parseInt($("#\\$qty-"+'\\'+pid).val(), 10);    
        var price = parseInt($("#\\$price-"+'\\'+pid).val(), 10);   
        var total = qty * price;        
        alert(total);    
    })
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <body>
      
    <div class='col-md-2'>$pro_name</div>
      
    <div class='col-md-2'><input type='text' class='form-control qty' pid='$pro_id' id='$qty-$pro_id' value='2'></div>
      
    <div class='col-md-2'><input type='text' class='form-control price' pid='$pro_id' id='$price-$pro_id' value='5' disabled></div>
      
    <div class='col-md-2'><input type='text' class='form-control total' pid='$pro_id' id='$total-$pro_id' value='$total' disabled></div>
      
    </body>

    【讨论】:

      猜你喜欢
      • 2012-11-07
      • 1970-01-01
      • 1970-01-01
      • 2014-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-10
      • 1970-01-01
      相关资源
      最近更新 更多