0

私の質問を明確にしましょう。

要素に5つのフィールドが含まれる要素を作成したいので、古い要素がnullの場合にユーザーが新しい要素を入れられるようにしたくないので、古い要素をループするときにアラートを作成し、存在するかどうかを確認しますいくつかの文字列。そうでない場合は、新しい要素を入れずにアラートを作成してください。すべてのフィールドに入力してください。

ここで再び私のコード

function addEvent() {
      var ni = document.getElementById('discount'); // Takes the a div named discount
      var discountForm = document.getElementById('discountForm'); // Takes the a form named discountForm
      var numi = document.getElementById('theValue'); // Takes the a hidden input field named theValue
      var num = (document.getElementById("theValue").value -1)+ 2; // Start counting to set the new divs form numbers
      numi.value = num;
      var divIdName = "my"+num+"Div"; // the new divs will be named
      var allDivTags = discountForm.getElementsByTagName('div'); // take all div tags
      var numOfDivs = (allDivTags.length -1); // take the number of the old div
      var oldDivIdName = document.getElementById(allDivTags[numOfDivs].id); // old div id
      var newdiv = document.createElement('div'); //the new div
      newdiv.setAttribute("id",divIdName);
      newdiv.innerHTML = "Company <select name=\"company[]\"><option value=\"\"></option><option value=\"ZI\">Avis</option><option value=\"ET\">Enterprise</option><option value=\"ZE\">Hertz</option><option value=\"ZD\">Budget</option><option value=\"ZR\">National</option><option value=\"AL\">Alamo</option></select> Discount Type <select name=\"type[]\"><option value=\"CD\">Discount Number</option><option value=\"PC\">Coupon Number</option></select> Code <input name=\"code[]\" type=\"text\"> Title <input name=\"title[]\" type=\"text\"> <a href=\"javascript:;\" onclick=\"removeElement(\'"+divIdName+"\')\">Remove</a>"; // creating the fileds in the new div
      ni.appendChild(newdiv);
      for(i=0; i<discountForm.elements.length;i++){ // loop through the divs
          if(numOfDivs != i-1){ // if tho old div exist and if the old div fields are empty
            if(oldDivIdName.children[i].value.length == 0){
              removeElement(divIdName); // then dont put the new one
              alert('Please enter all fields');
            }
          }
      }

    }

しかし、私の問題は、IEでエラーが発生することです。そのchildren[...].value.length is null or not an objectため、修正方法を理解しようとしています。

今、あなたにとってより明確になることを願っています。

4

2 に答える 2

2

あなたが私たちに与えた情報からそれを判断するのは非常に困難です。しかし、私の最初の推測は次のとおりです。

for(i=0; i<discountForm.elements.length;i++){
  if(numOfDivs != i-1){
    if(oldDivIdName.children[i].value.length == 0){
      removeElement(divIdName);
      alert('Please enter all fields');
    }
  }
}

あなたがやっている上:

oldDivIdName.children[i]

しかし、私が見ることができるフォーム内の要素の数として定義されています... oldDivIdNameの子の数ではありません。oldDivIdName よりも多くの要素がフォームにある場合、oldDivIdName.children[i] の値は null になります。また、null には「値」が定義されていません。

于 2012-06-24T22:45:40.437 に答える
0

あなたが私たちに与えた情報からそれを判断するのは非常に困難です。しかし、私の最初の推測は次のとおりです。

このメソッドを最初に呼び出すと、allDivTags.length が null または負の値であり、結果として要素が取得されません。

var oldDivIdName = document.getElementById(allDivTags[numOfDivs].id); 
于 2012-06-25T02:41:38.020 に答える