使用 JQuery 获取下拉选择值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19243368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get Dropdown selected value using JQuery
提问by MusicLovingIndianGirl
I use the following code to get the selected value of my dropdown using JQuery.
我使用以下代码使用 JQuery 获取下拉列表的选定值。
pStartMonth = $('#cboMonth1').val();
But I get the result as undefined
. What am I missing?
但我得到的结果为undefined
. 我错过了什么?
The HTML for my dropdown:
我的下拉列表的 HTML:
<asp:DropDownList ID="cboMonth1" runat="server" AutoPostBack="true" onclick="javascript:shouldsubmit=false;" ValidationGroup="vTimeSlot">
<asp:ListItem Value="0">-Select-</asp:ListItem>
<asp:ListItem Value="1">January</asp:ListItem>
<asp:ListItem Value="2">February</asp:ListItem>
<asp:ListItem Value="3">March</asp:ListItem>
<asp:ListItem Value="4">April</asp:ListItem>
<asp:ListItem Value="5">May</asp:ListItem>
<asp:ListItem Value="6">June</asp:ListItem>
<asp:ListItem Value="7">July</asp:ListItem>
<asp:ListItem Value="8">August</asp:ListItem>
<asp:ListItem Value="9">September</asp:ListItem>
<asp:ListItem Value="10">October</asp:ListItem>
<asp:ListItem Value="11">November</asp:ListItem>
<asp:ListItem Value="12">December</asp:ListItem>
</asp:DropDownList>
回答by Rory McCrossan
id
attributes of ASP.Net controls are generated server-side, so in your generated HTML, the id
would actually be something like _$ctrl0239023930
. What you need to use is ClientID
like this:
id
ASP.Net 控件的属性是在服务器端生成的,因此在您生成的 HTML 中,id
实际上类似于_$ctrl0239023930
. 你需要使用的是ClientID
这样的:
pStartMonth = $('#<%= cboMonth1.ClientID %>').val();
回答by Adil
The statement you have seems perfectly alright. you might be missing one or more of the following.
你的说法似乎完全没问题。您可能缺少以下一项或多项。
- Include jQuery library
- Put code in docuemt.ready
- Ensure you
- 包含 jQuery 库
- 将代码放入 docuemt.ready
- 确保您
EditBased on updated OP, as you have asp.net dropdown the id of dropdown will be changed in generated html so you need to use ClientID. You can also set ClientIDModeto static
to generate the same id as you have in server control.
编辑基于更新的 OP,因为您有 asp.net 下拉列表,下拉列表的 id 将在生成的 html 中更改,因此您需要使用 ClientID。您还可以将ClientIDMode设置static
为生成与服务器控制中相同的 ID。
$(document).ready(function(){
pStartMonth = $('#<%= cboMonth1.ClientID %>').val();
alert(pStartMonth );
});
ClientIDMode
客户端ID模式
ASP.NET provides multiple algorithms for how to generate the ClientID property value. You select which algorithm to use for a control by setting its ClientIDMode property. The algorithms are identified by the ClientIDMode enumeration values that are listed in the following table, MSDN.
ASP.NET 为如何生成 ClientID 属性值提供了多种算法。您可以通过设置其 ClientIDMode 属性来选择用于控件的算法。这些算法由下表MSDN中列出的 ClientIDMode 枚举值标识。
You can use the server side id
in javascript by setting ClientIDMode = "static"
您可以id
通过设置ClientIDMode = "static"在 javascript 中使用服务器端
HTML
HTML
<asp:DropDownList ID="cboMonth1" runat="server" ClientIDMode="static" AutoPostBack="true" onclick="javascript:shouldsubmit=false;" ValidationGroup="vTimeSlot">
Javascript
Javascript
pStartMonth = $('#cboMonth1').val();
回答by Kostrzak
If the javascript function is in .js file then use:
如果 javascript 函数在 .js 文件中,则使用:
$('select[id$="cboMonth1"]').val();
If it is in .aspx file than use:
如果它在 .aspx 文件中,则使用:
$('#<%= cboMonth1.ClientID %>').val();
回答by Prasanna Aarthi
Try this
尝试这个
$("#cboMonth1 option:selected").val();
回答by Reinstate Monica Cellio
If your script is in a file that is not parsed as ASP.Net (such as an included JS file), you can reference the element like this...
如果您的脚本位于未解析为 ASP.Net 的文件中(例如包含的 JS 文件),您可以像这样引用元素...
pStartMonth = $('[id$=cboMonth1]').val();
That will find an element with an ID that ends withcboMonth1
, which yours will.
这将找到一个 ID 以结尾的元素cboMonth1
,你会的。