2011-08-05 164 views
1

我想檢查表格中的特定單元格是否包含使用JavaScript的特定表單。檢查表格的單元格是否包含某種形式

這裏是我做了什麼:

for(var i = 1; i >= mainTable.tBodies[0].rows[2].length; i++) 
{ 
    if(mainTable.tBodies[0].rows[2].cells[i].elements[0] == document.getElementById("my_form")) 
    { 
     var cellIndex = i; 
     alert("cell" + i + " contains the form"); 
     break; 
    } 
} 

"my_form"實際上是內細胞數量1,但如果語句總是假的,儘管它應該在i = 1是真實的。我怎樣才能在JavaScript中實現這一點?

編輯:

這裏是整個代碼:

<HEAD> 
<SCRIPT LANGUAGE="JavaScript"> 
<!-- Begin 

//var mainTable = document.getElementById("mainTable"); 
var index_Skill = 1; 
var index_Speed = 1; 
var index_Age = 1; 
var index_Strength = 1; 
var index_Height = 1; 

function moveUp(theForm) 
{ 
    if(theForm == "form_Skill") 
    { 
     if(index_Skill == 1) 
     { 
      // get the max index of the table 
      var newIndex = Math.max(++index_Skill, ++index_Speed, ++index_Age, ++index_Strength, ++index_Height); 

      // insert a new row 
      var newRow = mainTable.tBodies[0].insertRow(newIndex); 

      // shift the rows one level down 
      for(var i = newIndex; i > 1; i--) 
      { 
       mainTable.tBodies[0].moveRow(i - 1, i); 
      } 

      // delete (skill) from the old index 
      var cellIndex; 
      for(var i = 1; i >= mainTable.tBodies[0].rows[2].length; i++) 
      { 
       if(mainTable.tBodies[0].rows[2].cells[i].elements[0] == document.getElementById("form_Skill")) 
       { 
        cellIndex = i; 
        alert(i); 
        break; 
       } 
      } 
      for(var i = cellIndex; i >= mainTable.tBodies[0].rows[2].length; i++) 
      { 
       mainTable.tBodies[0].rows[2].cells[i].innerHTML = mainTable.tBodies[0].rows[2].cells[i+1].innerHTML; 
      } 
      //mainTable.tBodies[0].rows[2].cells[mainTable.tBodies[0].rows[2].length].innerHTML = ""; 

      // add (skill) to the new index 
      var newCell = mainTable.tBodies[0].rows[1].insertCell(0); 
      newCell.innerHTML = 1; 
      newCell.setAttribute("align", "center"); 
      var newCell = mainTable.tBodies[0].rows[1].insertCell(1); 
      var obj = document.getElementById(theForm).cloneNode(true); 
      newCell.appendChild(obj); 

      // fixes the id's 
      for(var i = 1; i <= newIndex; i++) 
      { 
       mainTable.tBodies[0].rows[i].cells[0].innerHTML = i; 
       mainTable.tBodies[0].rows[i].cells[0].setAttribute("align", "center"); 
      } 

     } 
    } 
} 


function moveDown(theForm) 
{ 

} 


// End --> 
</script> 
</HEAD> 
<BODY> 

    <table id="mainTable" name="mainTable" border="1"> 

     <tr> 
      <th>Rank 
      </th> 
      <th colspan="5"> 
       <p align="center">The Criteria 
      </th> 
     </tr> 
     <tr> 
      <td> 
       <p align="center">1 
      </td> 

      <td> 
       <form name="form_Skill" method="post" style="margin: 0; text-align: center;"> 
        <input type="button" name="Up1" value="Up" onclick="moveUp('form_Skill');"> Skill 
        <input type="button" name="Down1" value="Down" onclick="moveDown('form_Skill');"> 
       </form> 
      </td> 

      <td> 
       <form name="form_Speed" method="post" style="margin: 0; text-align: center;"> 
        <input type="button" name="Up2" value="Up" onclick="moveUp('form_Speed');"> Speed 
        <input type="button" name="Down2" value="Down" onclick="moveDown('form_Speed');"> 
       </form> 
      </td> 

      <td> 
       <form name="form_Age" method="post" style="margin: 0; text-align: center;"> 
        <input type="button" name="Up3" value="Up" onclick="moveUp('form_Age');"> Age 
        <input type="button" name="Down3" value="Down" onclick="moveDown('form_Age');"> 
       </form> 
      </td> 

      <td> 
       <form name="form_Strength" method="post" style="margin: 0; text-align: center;"> 
        <input type="button" name="Up4" value="Up" onclick="moveUp('form_Strength');"> Strength 
        <input type="button" name="Down4" value="Down" onclick="moveDown('form_Strength');"> 
       </form> 
      </td> 

      <td> 
       <form name="form_Height" method="post" style="margin: 0; text-align: center;"> 
        <input type="button" name="Up5" value="Up" onclick="moveUp('form_Height');"> Height 
        <input type="button" name="Down5" value="Down" onclick="moveDown('form_Height');"> 
       </form> 
      </td> 

     </tr> 
    </table> 
</body> 
+0

您寫的javascript代碼。哪裏不對? – Naor

+0

@Naor如果您希望添加/刪除行,那麼表單所在行的索引是否也會發生變化,請參閱更新 –

+0

?即:在你的代碼中,你只是想循環遍歷第三行中的單元格。 –

回答

2

一個tr元素本身沒有一個.length財產。

此:

for(var i = 1; i >= mainTable.tBodies[0].rows[2].length; i++) 

...應該從cells而不是長度:

// right here -----------------------------------v 
for(var i = 1; i >= mainTable.tBodies[0].rows[2].cells.length; i++) 

而一個<td>沒有一個elements財產。我假定你打算.children,這取決於你支持的瀏覽器。

// ------------------------------------v 
mainTable.tBodies[0].rows[2].cells[i].children[0] 
+0

+1很好的指出了這一點(這就是爲什麼我建議使用.children代替DOM操作的屬性 –

+0

@Samuel:是的,只有'.children'屬性的問題在於它不支持Firefox 3(並且IE8和IE8之間存在不兼容問題,但只有當您包含註釋節點時才需要考慮) – user113716

+0

+1不錯,但我仍然無法獲取警報消息,如果if語句爲true,它應該彈出順便說一句,昨天我開始學習JavaScript,所以我可能在我的代碼中做了一些基本的錯誤) –

0

你有一個小錯字我猜指數:

for(var i = 0; i <= mainTable.tBodies[0].rows[2].length; i++) 
{ 
    if(mainTable.tBodies[0].rows[2].cells[i].elements[0] == document.getElementById("my_form")) 
    { 
     var cellIndex = i; 
     alert("cell" + i + " contains the form"); 
     break; 
    } 
} 
相關問題