如何在HTML.DropDownList中将id设置为选项 [英] How to set an id to option in HTML.DropDownList

查看:30
本文介绍了如何在HTML.DropDownList中将id设置为选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个HTML.DropDownList,我可以在其中设置datavaluefield和datatextfield,如下所示

<select>
<option value="Fruit">APPLE</option>
</select>

甚至,我还可以使用html属性将ID设置为下拉列表,但由于需要将‘ID’设置为如下选项,

 <select>
 <option ID='1' value="Fruit">APPLE</option>
 </select>

有没有人能分享一下这个想法.

推荐答案

您可以使用id属性将ID设置为option标记,并将this.value用于onchange元素的onchange回调。

下面是工作示例

document.getElementById("select1").onchange = function() {
    if (this.value == 'Fruit') {
        var optionID=document.getElementById('1');
        alert(optionID.value);

    }
    if (this.value == 'Flower') {
        var optionID=document.getElementById('2');
        alert(optionID.value);

    }
}
<select name="select01" id="select1" onchange="handleSelect()">
  <option value="Fruit" id="1">Apple</option>
  <option value="Flower" id="2">Rose</option>
</select>

这篇关于如何在HTML.DropDownList中将id设置为选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆