2013-07-31 42 views
3

我得到一个Javascript错误:对象#没有方法'getElementById'。我试图让一个按钮将选定元素传输到HTML中的另一个选择框。已经到处找,但小人物解决方案似乎为我工作= \Javascript错误:对象#<HTMLFormElement>没有方法'getElementById'

的Javascript

<script language="javascript"> 
function addDivision() 
{ 
    var selected = document.frmSubmit.getElementById("selectedDivisions"); 

    var div = document.frmSubmit.getElementById("divisions"); 
    var divId = div.options[div.selectedIndex].value; 
    var divText = div.options[div.selectedIndex].text; 

    var newOption = document.frmSubmit.createElement(divId); 
    newOption.text = divText; 

    selected.add(newOption,null); 
} 
</script> 

HTML

<div id="content"> 
<form id="frmSubmit" name="frmSubmit" action=""> 


<div id="Step1Content"> 
    <h2 style="float:left">Step 1:</h2><p style="float:left; padding-top:10px; padding-left:20px;">Select your divisions</p> 
    <div style="clear:both"> 
     <select id= "divisions" name="divisions" size="8"> 
    <? //Getting divisions based on League_id 
     $getDivisionsSQL = "Select * FROM level WHERE League_ID = '1' AND disabled = '0'"; 
     $getDivisionsQuery = $db->Query($getDivisionsSQL); 
     while($divRow = $db->GetRow($getDivisionsQuery)) 
     { 
      echo "<option id=".$divRow['id'].">".$divRow['description']."</option>"; 
     } 
    ?> 
     </select> 
    <? 
     echo "<img id='doAdd' width='40px' height='25px' style='position: relative; bottom:75px; cursor:pointer;' src='".$baseImagesPath."greenArrow.png'/>"; 
     echo "<img id='doAdd' width='40px' height='25px' cursor:pointer; style='position: relative; bottom: 25px; right:40px; cursor:pointer;' src='".$baseImagesPath."redArrow.png'/>"; 
    ?> 
     <select style="position:relative; right:40px;" name="selectedDivisions" id="selectedDivisions" size="8"> 
    <? //List of divisions to use 

    ?> <option>Apple</option> 
     </select> 
    </div> 

</div> 





<div style="padding-top:50px; padding-left:50px; width:100%; float:left; "> 
    <h2 style="float:left">Step 2:</h2><p style="float:left; padding-top:10px; padding-left:20px;">Select the starting date of games</p> 
</div> 

<div style="padding-top:50px; padding-left:50px; width:100%; float:left"> 
    <h2 style="float:left">Step 3:</h2><p style="float:left; padding-top:10px; padding-left:20px;">Select the total number of weeks in the season</p> 

<div style="padding-top:50px; width:100%; float:left"> 
    <input type="submit" value="Create Schedule"> 
</div> 
</div> 

</form> 
</div> 
+1

IDS是单数,你不能有多个具有相同ID的元素。 – epascarello

回答

3

这个问题发生,因为你正在尝试使用getElementById从一个节点元素,该方法属于document。这是合乎逻辑的,当你考虑它时:ID应该是唯一的在你的页面内,所以没有必要使用dom元素来通过id“过滤”一个元素。

因此,如果您更改:

document.frmSubmit.getElementById("selectedDivisions"); 

到:预期

document.getElementById("selectedDivisions"); 

一切都应该工作。


一点题外话,getElementsByTagNamegetElementsByClassName都支持节点元素 - 它们是用来选择符合规定的标签/类的名称,其所有子节点。

检查API参考: https://developer.mozilla.org/en-US/docs/Web/API/element

+0

感谢您的快速回复!现在我收到一个错误:无法读取属性'值'是未定义的。我知道我为每个条目赋予了一个值,这是使我失败的错误,但首先让我提交。 –

+0

正如我所说的,你不能在元素中使用'getElementById'(它不会工作),所以你应该使用'document.getElementById'而不是'document。 .getElementById'(也包括'var div')。 –

0

您正在尝试使用属于一个DOMDocument,而不是一个DOMElementMDN)的功能。

document.getElemendById()

代码的有效版本是actualy:

document.getElementById("selectedDivisions");

注: ID属性应该是独特,这意味着如果两个或多个元素具有相同的ID在你的文件中,该页面被认为是invalid by W3c

0

我会尝试为此使用jQuery。你可以从一个选择中抓取你想要的元素,并将它附加到另一个。

$('#selectedDivisions').append($('#divisions')); 
相关问题