2012-06-24 22 views
0

让我们澄清我的问题时,错误在IE循环

我想使该元素包含5个字段,所以我不希望用户应该能够把一个新的元素,如果旧的一个是空的元素,所以我通过老元素循环时,看看是否有一些字符串,如果没有则不要把新的元素,使警报请填写所有

这里再次我的代码所做的警报

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所以我试图找出如何解决它,

我希望它更清楚你现在。

+1

凡numOfDivs最初设置,或者是什么其目的? –

+0

发布更相关的代码。 –

+0

numOfDivs计算它的形式有多少个div – itsme

回答

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上定义。

+0

我刚更新了我的问题,请看看它现在对你更清楚了,谢谢你的时间 – itsme

0

这是很难从你给我们的信息告诉。但我的第一个猜测是:

当你第一次调用此方法,allDivTags.length是否为空或负值,导致 没有得到元素:

var oldDivIdName = document.getElementById(allDivTags[numOfDivs].id);