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個の非表示の入力が存在する可能性があります。存在しない入力をチェックした瞬間に、ループが中断するはずです。私のテストページには、プルされる2つの入力要素があります。3番目(null)に、firebugで次のエラーが発生します。

prevDoc.getElementById(CurrentPrice) is null

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

はい、それは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

値が null になることはありません。

値が入力されていない場合、値は "" または長さゼロになります。

要素が存在しない場合は、要素の存在を確認します。

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 に答える