简体   繁体   English

lint警告val()和text()被错误地调用

[英]lint warning that val() and text() are called incorrectly

I have: 我有:

 TotalPrice = parseInt(TotalPrice*100)/100;
 $('input[name=EstimatedPrice]').val(TotalPrice);
 $('#EstimatedPriceDisplay').text(TotalPrice);

and I'm getting two warnings from lint. 而且我从lint得到了两个警告。

  1. val() called incorrectly val()调用不正确

  2. text() called incorrectly. text()调用不正确。

I was able to eliminate the text() called incorrectly error by doing the following: 通过执行以下操作,我能够消除错误调用的text()错误:

$('#EstimatedPriceDisplay').text('' + TotalPrice);

But that seems kinda kludgy to me. 但这对我来说似乎有些愚蠢。

Doing: 这样做:

$('#EstimatedPriceDisplay').text('' + TotalPrice);

should be fine or you can use the toString method: 应该没问题,或者你可以使用toString方法:

$('#EstimatedPriceDisplay').text(TotalPrice.toString());

Do you want to support IE <5.5? 你想支持IE <5.5吗? If not, try to use the .toFixed() method , which returns a string and rounded to the specified decimal place. 如果没有,请尝试使用.toFixed()方法 ,该方法返回一个字符串并四舍五入到指定的小数位。

 TotalPrice = TotalPrice.toFixed(2);

Since you're reusing the variable, I'd make it a String when the value is assigned. 由于您正在重用该变量,因此在分配值时将其设为String。

TotalPrice = (Math.round(TotalPrice*100)/100) + '';

This way you only need to do it once, and it is not cluttering .val() and .text() 这样你只需要做一次,它不会混乱.val().text()

$('input[name=EstimatedPrice]').val(TotalPrice);
$('#EstimatedPriceDisplay').text(TotalPrice);

EDIT: Changed to Math.round() to get proper up/down rounding. 编辑:更改为Math.round()以获得正确的向上/向下舍入。

 $('input[name=EstimatedPrice]').val(TotalPrice.toFixed(2));
 $('#EstimatedPriceDisplay').text(TotalPrice.toFixed(2));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM