无法获取JavaScript来检查null对象

Cou*_*phy 0 javascript

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

          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;
              }
          }
Run Code Online (Sandbox Code Playgroud)

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

prevDoc.getElementById(CurrentPrice) is null

 if (prevDoc.getElementById(CurrentPrice).value != null) {
Run Code Online (Sandbox Code Playgroud)

是的它是空的...这就是check_ಠ的检查

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

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

Jam*_*lin 5

if (prevDoc.getElementById(CurrentPrice).value != null) {
Run Code Online (Sandbox Code Playgroud)

可以扩展到:

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

if (value != null) { 
Run Code Online (Sandbox Code Playgroud)