0

我真的不知道这里有什么问题。据我所知,代码很简单,应该可以正常工作。

          var Prices="";
          for (var PriceCount = 1; PriceCount <= 120; PriceCount++) {
              var CurrentPrice = "Price" + PriceCount;
              if (prevDoc.getElementById(CurrentPrice).value != null) {
                  if (Prices == "") {
                      Prices = prevDoc.getElementById(CurrentPrice).value;
                  } else {
                      Prices += "," + prevDoc.getElementById(CurrentPrice).value;
                  }
              } else {
                  break;
              }
          }

表单上最多可以有 120 个隐藏输入。当我们检查一个不存在的输入时,循环应该会中断。我的测试页面有两个被拉出的输入元素。在第三个(null)上,我在 firebug 中收到此错误:

prevDoc.getElementById(CurrentPrice) is null

 if (prevDoc.getElementById(CurrentPrice).value != null) {

是的,它是空的……这就是 ಠ_ಠ 的检查</p>

有谁知道我做错了什么?这似乎应该是非常直截了当的。

编辑:为清楚起见,prevDoc=window.opener.document

4

4 回答 4

5
if (prevDoc.getElementById(CurrentPrice).value != null) {

可以扩展为:

var element = prevDoc.getElementById(CurrentPrice);    
var value = element.value; /* element is null, but you're accessing .value */

if (value != null) { 
于 2012-06-05T22:38:51.443 回答
1

值永远不会为空。

如果未填写,则值为“”或长度为零。

如果该元素不存在,您将检查该元素是否存在。

var CurrentPrice = "Price" + PriceCount;
var elem = prevDoc.getElementById(CurrentPrice);
if (elem && elem.value != null) {
于 2012-06-05T22:42:07.280 回答
0

尝试

if (prevDoc.getElementById(CurrentPrice) !== null)
于 2012-06-05T22:38:23.333 回答
0

我认为应该是:

var Prices="";
for (var PriceCount = 1; PriceCount <= 120; PriceCount++) {
    var CurrentPriceId = "Price" + PriceCount,
        CurrentPrice = prevDoc.getElementById(CurrentPriceId);

    if (CurrentPrice != null) {
        Prices = (Prices == "") ? CurrentPrice.value : (Prices + "," + CurrentPrice.value);
    }
    else break;
}
于 2012-06-05T22:48:48.540 回答