使用 jQuery .val() 设置时在 <input> 中显示为 NaN 的数字
Number showing as NaN in <input> when set with jQuery .val()
我正在尝试使用jQuery设置输入字段的值val()
$('[name="paytype1"]').change(function(){
var total = big_total();
total = parseFloat(total);
total = total.toFixed(2);
console.log(total);
if(isNaN(total)) {
alert(total);
}
$('[name="pay1amt"]').val(total);
})
浏览器中显示的所有内容都是 NaN
.
运行console.log(total)
显示一个数字,例如 61.00,并且没有触发警报。
big_total()
函数正确返回数字(它根据其他输入字段中的数字计算)。
function big_total() {
var big_total = 0;
$('.total').each(function(i){
big_total = big_total + parseFloat($(this).val());
});
big_total = big_total.toFixed(2)
return big_total;
}
在 ajax 调用后,jQuery 将带有类总数的输入动态添加到页面中。 这是插入的 html:
<div class="grid_12 alpha omega line-items">
<div class="grid_1">CH02<input type="hidden" name="item_code[]" value="CH02">
<input type="hidden" name="item_id[]" value="1458">
</div>
<div class="grid_3">
DVA Regular consult
<input type="hidden" name="item_desc[]" value="Subsequent consultation">
<input type="hidden" name="type[]" value="Service">
</div>
<div class="grid_1">
<input type="text" size="3" name="qty[]" value="1" class="qty" max-length="3">
</div>
<div class="grid_2">
<input type="text" size="7" name="sub_total[]" value="61.00" class="sub-total">
</div>
<div class="grid_1">
<select name="tax[]">
<option value="$taxitems[ID]">0%</option>
<option value="$taxitems[ID]">10%</option>
</select>
</div>
<div class="grid_2">
<input type="text" size="7" name="total[]" value="61.00" class="total" readonly="">
</div>
<div class="grid_1 omega">
<div class="remove_stock_line"><img src="images/collapse.png"></div>
</div>
</div>
这是触发函数的下拉列表:
<div class="grid_8 alpha omega">
<div class="grid_3 alpha">
<select name="paytype1" class="pay_type">
<option default="" value="nill"></option>
<option value="cash">cash</option>
<option value="HICAPs">HICAPs</option>
<option value="EFTPOS">EFTPOS</option>
<option value="Visa">Visa</option>
<option value="Mastercard">Mastercard</option>
<option value="American Express">American Express</option>
<option value="Other card">Other card</option>
<option value="EFT">EFT</option>
<option value="Cheque">Cheque</option>
</select>
</div>
<div class="grid_3 omega">
<input type="text" size="10" name="pay1amt" value="" onclick="select();" class="pay_values">
</div>
</div>
思潮?
这个函数没有任何问题。但是由于我们无法看到您的完整源代码,并且您没有共享您的big_total
函数,因此我们只能推测。根据我的个人经验,我只看到两种可能的情况。
-
big_total()
返回一个非数字值或具有像$4339
这样的前导非数字字符,因为parseFloat()
忽略了除 period(.) 之外的尾随非数字字符,例如:83483a;lsdfad
不会给你错误。但是,由于在控制台或警报中看不到任何错误,因此可以排除这种情况。 - 唯一的其他解释是,代码的其他部分正在覆盖此函数设置的值。
如果我是你,我会按照以下步骤进行调试。
我会替换:
var total = big_total()
跟:
var total = 1234.56;
如果您在$('[name="pay1amt"]')
中看到结果,则问题出在big_total
中。如果您仍然看到 NaN,那么我会查看代码中的其他地方以查看值被覆盖的位置。
这与问题无关,但非常重要。
不要在函数 big_total()
中使用变量名big_total
。Javascript 在从堆栈中存储和检索函数和变量时不区分它们。括号指示解释器执行函数,而括号的缺失指示解释器将其作为变量引用(不执行它)。
//Try this code
function hi(){
//without parenthesis it is treated as a variable
alert('without ()'n'n'+hi);
return 6;
}
//with parenthesis it is executed as a function
alert('With () 'n'+ hi());
这正在工作
function big_total() {
var bg_total = 0;
$('.total').each(function(i){
bg_total = bg_total + parseFloat($(this).val());
});
big_total = bg_total.toFixed(2)
return bg_total;
}
$(document).ready(function(){
$("[name='paytype1']").change(function(){
var total = big_total();
total = parseFloat(total);
total = total.toFixed(2);
console.log(total);
if(isNaN(total)) {
alert(total);
}
$('input[name="pay1amt"]').val(total);
});
});
更新
在 big_total()
函数中,$('.total')
元素只是输入的一个类,其中出现次数为 1。 你为什么要用$.each
$('.total').each(function(i){
big_total = big_total + parseFloat($(this).val());
});
//you can just replace the use of .each above to this in the big_total function, except you have special reason for that
bg_total = bg_total + parseFloat(".total").val());
<input type="text" size="7" name="total[]" value="61.00" class="total" readonly="">
当你在问题中说And all that is being shown in the browser is NaN. Running console.log(total) shows a number, e.g. 61.00, and no alert is being triggered.
- 仅当类
.total
的输入值以非数字开头时,才会显示 NaN。 那只是当您期望if(isNaN(total)) {alert(total);}
工作时。 - 是的,
console.log(total)
显示值 61.00,因为 big_total() 函数正确返回数字,但我不知道您是否已经处理好(it calculates from numbers in other input fields)
除此之外,您的问题应该得到解决。
- 通过单击表单中的按钮,在代码生成中使用javascript生成字母数字代码
- 测试索引值是否等于某个数字的倍数
- 正在将base64 jpeg从input-type=file上传到服务器
- 将数字转换为一定数量的硬币
- 键入最后一位数字后自动提交
- 如何在javascript中迭代数字列表
- Javascript逻辑运算符和字符串/数字
- 如何在javascript中获得与特定数字相等的随机数
- 如果元素's的ID以数字开头
- 递增一个数字而不去掉前导零
- 如何使用JavaScript查找1和N之间的所有数字的总和
- 如何在这里将两个值最低的数字相加
- 如何删除除冒号、数字和'上午'或者'下午'
- 如何为jQuery屏蔽输入插件创建一个允许字母数字、空格和重音字符的掩码
- Javascript排序字符串或数字
- 我想在AngularJS应用程序中创建一个输入数字框,用户不应该在该框上键入十进制数字.(一个整数输入框)
- 将数字四舍五入到小数点后两位
- HTML 5”;数字“;键入小数位时,type input没有值
- 在Angular中,如何使用input type=text字段来只接受数字、负号和小数,别无其他
- 从一个特定的INPUT Javascript获取数字