0

我一直在尝试从客户端获取输入文本框的值,如下所示:

var CKQtyToDate = XSP.getElementById("#{id:CKQtyToDate}");
var CKQtyToDate = XSP.getElementById("#{id:CKQtyToDate}");
var SLQtyToDate = XSP.getElementById("#{id:SLQtyToDate}");
var FinishingQtyToDate = XSP.getElementById("#{id:FinishingQtyToDate}");
var PackingQtyToDate = XSP.getElementById("#{id:PackingQtyToDate}"); 

if ((parseInt(CKQtyToDate.value)+parseInt(SLQtyToDate.value)+parseInt(FinishingQtyToDate.value)+parseInt(PackingQtyToDate.value))>parseInt(hContractQty.value))
{
    alert("Total qty more than contract qty! =" + parseInt(CKQtyToDate.value)+parseInt(SLQtyToDate.value)+parseInt(FinishingQtyToDate.value)+parseInt(PackingQtyToDate.value));
    return false;
}

但是我仍然得到只有字符串连接的结果,我怎么能通过这个东西???

4

1 回答 1

0

首先,在 Javascript 中,parseInt 在解析空字符串时返回 NaN。因此,请确保在进行计算之前测试空字符串。您可以测试字符串是否可以使用!isNaN(string).

我相信您的问题是您在警报框中连接字符串。我建议您将计算移至一个单独的变量,然后在警报框中将消息字符串与此变量连接起来:

var result = parseInt(CKQtyToDate.value)+parseInt(SLQtyToDate.value)+parseInt(FinishingQtyToDate.value)+parseInt(PackingQtyToDate.value);

if (result>parseInt(hContractQty.value))
{
   alert("Total qty more than contract qty! =" + result);
   return false;
}
于 2012-06-25T11:15:09.577 回答