Jquery回归NaN,有什么想法吗?

Ste*_*ven -2 jquery

HTML代码:

<form id="hostadd" action="addmoney.php?taskid=12" method="post" onSubmit="return false">
<input type="hidden" name="mode" value="" />
current unit price:$
<input id="unitprice" name="unitprice"  type="text" size="5" maxlength="6" value= 0.50 />&nbsp
Shortfall:<input id="shortfall" type="text"  name="shortfall" size="4" maxlength="6" value=80 />
<input id="add_price" type="submit" value="Submit" onclick="Adjust()"/></form
Run Code Online (Sandbox Code Playgroud)

Jquery代码:

 function Adjust(){
        $totalprice=parseFloat($('unitprice').val())*parseInt($('shortfall').val());
          alert($totalprice);

        };
Run Code Online (Sandbox Code Playgroud)

当我测试

var unitPrice = $('#unitprice').val();
alert(unitPrice);
var shortFall = $('#shortfall').val();
alert(shortFall);
Run Code Online (Sandbox Code Playgroud)

我收到两个空白警报.

Jam*_*man 6

您正在寻找HTML'缺口'和'unitprice':

<shortfall></shortfall>
<unitprice></unitprice>
Run Code Online (Sandbox Code Playgroud)

你的意思是这样吗?也许你的课后或ID选择?

$('.unitprice').val() //class
$('#unitprice').val() //id
$('.shortfall').val() //class
$('#shortfall').val() //id
Run Code Online (Sandbox Code Playgroud)

尝试查看每个组件以查看哪个组件出现故障.

alert($('#unitprice').val());
alert(parseFloat($('#unitprice').val()));
alert($('#shortfall').val());
alert(parseInt($('#shortfall').val(),10);
Run Code Online (Sandbox Code Playgroud)

另外,将基数传递给parseInt()(注意:下面我假设你想要小数,所以使用10)

parseInt($('#shortfall').val(),10)
Run Code Online (Sandbox Code Playgroud)

虽然默认值是10,但危险的是不要规定它.如果传递零填充数字,则praseInt采用Octal.

alert(parseInt(010)); // alerts '8'
alert(parseInt(010),10); // alerts '10'
Run Code Online (Sandbox Code Playgroud)