2012-08-07 49 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。

相关问题