如何从HTML SELECT中获取选择的项目名称? [英] How to get select item name from html SELECT?

查看:446
本文介绍了如何从HTML SELECT中获取选择的项目名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < SELECT id =MySelectionname =MySelectionrunat =serverDataValueField =IDDataTextField =Name>< / SELECT> 

数据是通过设置其属性DataSource来提供的:

  MySelection.DataSource = IEnumerable集合数据

假设数据存储在数据库中的表中:


  1. 选项A

  2. 选项B

  3. 选项C

  4. 选项D

MySelection.Value ,我可以得到ID值,例如1,2,3。



MySelection。名称,我会得到我的选择,而不是像选项A这样的选定项目名称



如何获取选定的项目Name? / p>

解决方案

有没有理由不使用ASP.NET DropDownList?



为了从select元素中获取文本,您应该可以使用:

  MySelection.Items [ MySelection.SelectedIndex]。文本; 


Use Html element SELECT in aspx page like:

<SELECT id="MySelection" name="MySelection" runat="server" DataValueField="ID"  DataTextField="Name"></SELECT>

Data is feed by setting its property DataSource:

MySelection.DataSource = IEnumerable collection Data

Suppose data is stored in table in DB like:

  1. Option A
  2. Option B
  3. Option C
  4. Option D

From MySelection.Value, I can get the ID value, like 1,2,3.

From MySelection.Name, I will get "MySelection", not the selected item name like "Option A"

How to get the selected item Name?

解决方案

Is there a reason that you aren't using the ASP.NET DropDownList?

In order to get the text from a select element you should be able to use:

MySelection.Items[MySelection.SelectedIndex].Text;

这篇关于如何从HTML SELECT中获取选择的项目名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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