jQuery 从asp.net的下拉列表中获取选定的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8643603/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 10:49:18  来源:igfitidea点击:

Getting the selected value from dropdownlist in asp.net

jqueryasp.netdrop-down-menu

提问by Ktt

I have been trying to get the selected item value from a dropdownlist but it seems like it is not going to work. I have looked at the other topics and I know that this question has been asked several times, but I need help. I have tried the following code:

我一直在尝试从下拉列表中获取所选项目的值,但它似乎不起作用。我查看了其他主题,我知道这个问题已被问过多次,但我需要帮助。我尝试了以下代码:

$('ddlWorkHourFact option:selected').val()

but it returns me "undefined" and I don't know why.

但它返回我“未定义”,我不知道为什么。

Here is my dropdownlist:

这是我的下拉列表:

<asp:DropDownList ID="ddlWorkHourFact"  runat="server">
                            <asp:ListItem Value="7" Text="7">7</asp:ListItem>
                            <asp:ListItem Value="8" Selected="True" Text="8">8</asp:ListItem>
                            <asp:ListItem Value="9" Text="9">9</asp:ListItem>
                        </asp:DropDownList>

回答by Remy

First, make sure that the ID ddlWorkHourFact exists in the HTML code. ASP.NET often creates something like this: ctl1_ddlWorkHourFact. You can use

首先,确保 ID ddlWorkHourFact 存在于 HTML 代码中。ASP.NET 经常创建这样的东西:ctl1_ddlWorkHourFact。您可以使用

ClientIDMode="Static"

to avoid that problem. Afterwards

为了避免这个问题。然后

$('#ddlWorkHourFact').val()

should be enough.

应该够了。

回答by Bruno Costa

Ktt,

克特,

You need to understand that ASP.NET will generate a different ID in client-side if you are putting the control inside a content, etc. This is done because the ID in client-side should be unique. If you are using ASP.NET 4.0 you can do what Remy told you.

您需要了解,如果您将控件放在内容等中,ASP.NET 将在客户端生成不同的 ID。这样做是因为客户端中的 ID 应该是唯一的。如果您使用的是 ASP.NET 4.0,您可以按照 Remy 告诉您的去做。

If you are not using ASP.NET 4.0, you can't do that, but you can do a "workaround" in jquery.

如果您不使用 ASP.NET 4.0,则不能这样做,但您可以在 jquery 中执行“解决方法”。

function GetClientID(id, context) {
var el = $("#" + id, context);
if (el.length < 1)
el = $("[id$=_" + id + "]", context);
return el;
}

For more information on this code you can go here.

有关此代码的更多信息,您可以转到此处

Then, you only need to do something like: GetClientID("txtboxId").attr("id") to get the ID.

然后,您只需要执行以下操作:GetClientID("txtboxId").attr("id") 即可获取 ID。

$("#" + GetClientID("ddlWorkHourFact").attr("id") + " option:selected").val();

This is only a example, you can improve the code.

这只是一个例子,你可以改进代码。

Edit:

编辑:

You can also use something like this, if you are doing that in same page of control.

如果您在同一控制页面中这样做,您也可以使用类似的方法。

$('#<%= ddlWorkHourFact.ClientID %> option:selected').val();

回答by NiK

try $('#ddlWorkHourFact :selected').val()

尝试 $('#ddlWorkHourFact :selected').val()

seems like you are missing the hash "#" symbol...

似乎您缺少哈希“#”符号...

回答by dku.rajkumar

try something like

尝试类似

$('#ddlWorkHourFact').options[$('#ddlWorkHourFact').selectedIndex].val();

回答by reshma k

try this :

尝试这个 :

   var _ddl = $("[id$='_ddlWorkHourFact']").attr("id");
    _option = "#" + _ddl + " option:selected";
    _value= $(_option).val();

回答by Ktt

okay I figured the problem. I was trying to alert it when the page loads. script was running when the page starts. but I put the alert in a function and give it as an onclick, then the problem is over now. Thanks for helps

好吧,我想出了问题。我试图在页面加载时提醒它。页面启动时脚本正在运行。但是我将警报放在一个函数中并将其作为 onclick 提供,那么问题现在就结束了。感谢您的帮助