Ids sind einzigartig, so dass Sie nicht eine einzelne, indem auswählen, was Sie ausgewählt. Es gibt viele Möglichkeiten, dies zu tun. Eine Möglichkeit besteht darin, das Ereignis zu übergeben und das Ziel zu lesen.
function sayHelloWorld(event) {
var sel = event.target, //the select that was active
selIndex = sel.selectedIndex,
value = sel.options[selIndex].value;
console.log(selIndex, value);
}
<section>
<select id='myDropDown1' onchange='sayHelloWorld(event)'>
<option value='' disabled selected>CHOOSE ONE</option>
<option id='' value='cows'>COWS</option>
<option id='' value='pigs'>PIGS</option>
<option id='' value='chicks'>CHICKS</option>
</select>
</section>
<section>
<select id='myDropDown2' onchange='sayHelloWorld(event)'>
<option value='' disabled selected>CHOOSE ONE</option>
<option id='' value='cows'>COWS</option>
<option id='' value='pigs'>PIGS</option>
<option id='' value='chicks'>CHICKS</option>
</select>
</section>
Sie können das aktuelle Objekt mit this
function sayHelloWorld(sel) {
var selIndex = sel.selectedIndex,
value = sel.options[selIndex].value;
console.log(selIndex, value);
}
<section>
<select id='myDropDown1' onchange='sayHelloWorld(this)'>
<option value='' disabled selected>CHOOSE ONE</option>
<option id='' value='cows'>COWS</option>
<option id='' value='pigs'>PIGS</option>
<option id='' value='chicks'>CHICKS</option>
</select>
</section>
<section>
<select id='myDropDown2' onchange='sayHelloWorld(this)'>
<option value='' disabled selected>CHOOSE ONE</option>
<option id='' value='cows'>COWS</option>
<option id='' value='pigs'>PIGS</option>
<option id='' value='chicks'>CHICKS</option>
</select>
</section>
passieren Oder Sie können Ereignisse ohne die Inline-Event-Handler
hinzufügen
function sayHelloWorld() {
var sel = this,
selIndex = sel.selectedIndex,
value = sel.options[selIndex].value;
console.log(selIndex, value);
}
var sels = document.querySelectorAll('.selNav');
for (var i=0; i<sels.length;i++) {
sels[i].addEventListener("change", sayHelloWorld);
}
<section>
<select class="selNav" id='myDropDown1'>
<option value='' disabled selected>CHOOSE ONE</option>
<option id='' value='cows'>COWS</option>
<option id='' value='pigs'>PIGS</option>
<option id='' value='chicks'>CHICKS</option>
</select>
</section>
<section>
<select class="selNav" id='myDropDown2' >
<option value='' disabled selected>CHOOSE ONE</option>
<option id='' value='cows'>COWS</option>
<option id='' value='pigs'>PIGS</option>
<option id='' value='chicks'>CHICKS</option>
</select>
</section>
So, als Sie die console.log Linien ändern würde
window.location.href = "externalPHPfile.php?w1=" + selIndex + "&w2=" + stuff;
oder
window.location.href = "externalPHPfile.php?w1=" + encodeURIComponent(value) + "&w2=" + stuff;
, dass dieses Zeug sein wird unter der Annahme in Ihrem Code gültig ist, dass Sie werden nicht angezeigt.
IDs sind Singular. – epascarello