<select class="dName" name="dName">
<option SELECTED value="#" DISABLED>========================</option>
<option id="0" value="amensah">Abbey-Mensah, Michael</option>
<option id="1" value="acharya">Acharya, Niraj</option>
<option id="2" value="achonu">Achonu, Geoffrey C.</option>
<option id="3" value="agustin">Agustin, Erie</option>
<option id="4" value="agyemang">Agyemang, Kuragu</option>
</select>
我在 Javascript 中使用 ID:
if (i[iVar] == $(".dName").find('option:selected').attr('id')) {
拉出选择的选项。
现在假设我想删除 dName 选择的选项 #2,而不是将 #3 更改为 #2 作为 ID,将 #4 更改为 #3 作为 ID,我可以使用数组吗?这可能很简单,但有 100 多个选项会非常耗时
是否有办法更改 dName 选择中的 ID,以便无论它用作数组?像这样的东西:
<option id="[]" value="amensah">Abbey-Mensah, Michael</option>
那么如果我删除列表中间的一个选项,它会保持编号结构的顺序吗?
已解决:
if (i[iVar] == $(".dName").find('option:selected').attr('id')) {
已更改为:
if (i[iVar] == "the value of selection option") {
和
document.getElementById('first').innerHTML = phyList[$(".dName").find('option:selected').attr('id')].firstName;
已更改为:
document.getElementById('first').innerHTML = phyList[$(".dName").find('option:selected').index()-1].firstName; //.index()-1 because the first option is "=" which is not being used
这样我就可以完全摆脱 ID,不用担心删除选项语句。
最佳答案
您根本不需要 ID,无论您在 select
元素中删除什么内容(.remove() ?),都会采用 index
所选选项
的:
举个例子:
$('select').on('change', function(){
alert( $(':selected').index() );
});
关于jquery - 使用 HTML ID 作为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16319671/