如何在html select元素中获取所选项目的文本? [英] How can I get the text of the selected item in an html select element?

查看:175
本文介绍了如何在html select元素中获取所选项目的文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要html select元素中所选项目的值. 此处已接受的答案建议这样做:

I need the value of the selected item in an html select element. Here the accepted answer recommends doing it like so:

Request.Form["testSelect"]

因此,就我而言,我有这个html select元素:

So in my case, I've got this html select element:

<select name="subcategory" color="<%=Session("TextColor")%>" style="font: 8pt arial" onchange="UpdateFlag=true;">
    <% if Not IsNewBusiness then %>
      <option <% If subcategory = "0" Then Response.Write(" selected")%> value="0">Existing
      <option <% If subcategory = "1" Then Response.Write(" selected")%> value="1">Organic Growth
    <% else %>
      <option <% If subcategory = "0" Then Response.Write(" selected")%> value="0">New
      <option <% If subcategory = "1" Then Response.Write(" selected")%> value="1">Assumed
    <% end if %>
</select>

然而,设计时编译器对此并不满意,如"Request.Form"下面的花招和"subcategory"的白色所显示的那样:

Yet the design-time compiler is not happy with it, as can be seen by the squiggles beneath "Request.Form" and the whiteness of "subcategory":

我需要怎么做才能从VB.NET中获取该价值?

What do I need to do to grab that value from VB.NET?

当我尝试Sailor在他的答案中建议的内容时(在HTML select元素中添加了"runat ="server"),我得到了:

When I tried what Sailor suggested in his answer (added "runat="server"" to the html select element), I got:

Server Error in '/EMS/customerreportingnet' Application.
--------------------------------------------------------------------------------

Parser Error 
Description: An error occurred during the parsing of a resource required to service this request. Please review the following 

specific parse error details and modify your source file appropriately. 

Parser Error Message: Server tags cannot contain <% ... %> constructs.

Source Error:    

Line 744:                                        </td>
Line 745:                                        <td nowrap align="left" valign="top">
Line 746:                                            <select runat="server" name="subcategory" color="<%=Session

("TextColor")%>" style="font: 8pt arial" onchange="UpdateFlag=true;">
Line 747:                                                <% If Not IsNewBusiness Then%>
Line 748:                                                <option <% If Subcategory = "0" Then Response.Write(" selected")%> 

value="0">     

Source File: /EMS/customerreportingnet/pages/custmaint_entry.aspx    Line: 746     

--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.5485; ASP.NET Version:2.0.50727.5491

删除它消除了错误.

此:

Subcategory = Request.Form.Item("selectSubcategory")

...正在给我所选项目的索引(例如"0");我该如何获取实际文本呢?

...is giving me the index of the selected item (such as "0"); how can I get the actual text instead?

事实证明,这实际上并不是给我所选项目的索引,毕竟-只是给我总为"0"-选择了第一个(0th)还是第二个(1st)选项.

It turns out that it's not really giving me the index of the selected item, after all - it is simply giving me "0" always - whether the first (0th) or second (1st) option is selected.

如果我尝试一开始提到的内容,请使用以下代码:

If I try what is mentioned at the outset, using this kind of code:

Subcategory = Request.Form["selectSubcategory"]

我收到此错误:

Compilation Error 
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: BC30311: Value of type 'System.Collections.Specialized.NameValueCollection' cannot be converted to 'String'.

Source Error:

Line 90:         CustNo = Request.Form.Item("CustNo")
Line 91:         'Subcategory = Request.Form.Item("selectSubcategory").ToString() '<= question at https://stackoverflow.com/questions/42655000/how-can-i-reference-the-selected-item-in-an-html-select-element-vb-net
Line 92:         Subcategory = Request.Form["selectSubcategory"]
Line 93:         AutoID = Request.Form.Item("AutoID")
Line 94:         ReturnMsg = ""

Source File: C:\Users\cshannon\Source\Workspaces\CSReports\EnhancedMonthlySalesReporting\customerreportingnet\customerreportingnet\pages\custmaint_entry.aspx    Line: 92

这就是为什么我走另一条路线,原因是:

That's why I went the other route, with:

Subcategory = Request.Form.Item("selectSubcategory").ToString()

...但是那也不足以榨菜(总是总是返回0).

...but that's not quite cutting the mustard, either (just returning 0 always).

推荐答案

确保您的表单标签中有runat="server".否则,请将其保留在选择"标签中.

Make sure you have runat="server" in your form tag. Otherwise keep it in the Select tag.

然后尝试使用

Request.Form.Item("Subcategory")

这篇关于如何在html select元素中获取所选项目的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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