2012-08-07 48 views
0

我想選擇所有下拉與從主下拉列表中選擇的值相同。如果在主下拉列表中選擇了一個選擇,但是它有效,但是如果有兩個選擇,則不起作用,下面爲您的信息添加了一些代碼。選擇第一個下拉爲與其他下拉相同的值

HTML:

<select name="ForceSelection" id="ForceSelection" onChange="javascript:return setDropDown();"> 
<option value="" selected>Select Name</option> 
<option value="Pass">Pass</option> 
<option value="Fail">Fail</option> 
</select> 

<select id="Qualifications" name="Qualifications"> 
    <option value="select">select</option> 
    <option value="Pass">Pass</option> 
    <option value="Fail">Fail</option> 
</select> 

<select id="Qualifications" name="Qualifications"> 
    <option value="select">select</option> 
    <option value="Pass">Pass</option> 
    <option value="Fail">Fail</option> 
</select> 

的JavaScript:

function setDropDown() { 
    var index_name=document.QualificationForm.ForceSelection.selectedIndex; 

    document.QualificationForm.Qualifications.selectedIndex=index_name; 

} 
+1

你有兩個具有相同'id'的元素。這是無效的HTML,在文檔中'id' ***必須是唯一的。 – 2012-08-07 11:29:29

+0

您的HTML無效...您不能擁有兩個ID爲'Qualifications'的元素 – freefaller 2012-08-07 11:29:42

+0

是什麼讓您選擇了所有這些不相關的標籤?這是純粹的JavaScript問題。 – 2012-08-08 06:26:50

回答

0

試試這個

function setDropDown() { 
    var index_name = 
     document.getElementsByName('ForceSelection')[0].selectedIndex; 
    var others = document.getElementsByName('Qualifications'); 
    for (i = 0; i < others.length; i++) 
     others[i].selectedIndex = index_name; 
} 
+0

非常感謝你,這是第一次。真的很感激迅速的迴應。 – 2012-08-07 12:55:15

0

你可能使用以下,但目前未經測試:

function setDropDown(el){ 
    if (!el) { 
     return false; 
    } 
    else { 
     var index = el.selectedIndex, 
      selects = document.getElementsByName('qualifications'); 
      for (var i=0, len=selects.length; i<len; i++){ 
       selects[i].selectedIndex = index; 
      } 
    } 
} 

這需要你通過#ForceSelectionselect元素融入功能,所以被稱爲像:

<select name="ForceSelection" id="ForceSelection" onChange="javascript:return setDropDown(this);"> 
<!-- other stuff --> 
</select> 

這傳入元素的selectedIndex將應用於其他select元素與qualifications名稱。

此外,請允許我重申:一個id必須是文檔中唯一以有效的HTML。

相關問題