如何使用 jquery 或 javascript 从 Telerik radcombobox 获取选定的 VALUE?val() 不工作

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

how to use jquery or javascript to get the selected VALUE from telerik radcombobox? val() not working

javascriptjqueryasp.nettelerik

提问by Sinaesthetic

I have a listview that has a nested listview which contain radcomboboxes in the itemtemplate. As such, the IDs (as far as I know) are useless to me.

我有一个列表视图,它有一个嵌套的列表视图,其中在 itemtemplate 中包含 radcomboboxes。因此,ID(据我所知)对我来说毫无用处。

For example, if I have 30 items, each one of those items is going to generate a new combobox so the names are going to be generated by asp. What I need is to be able to grab the selected value from whichever combobox is being worked by the user. I'm currently using jQuery and some absurd parent().parent().children()type nonsense in order to find the correct combobox in relation to the submit button.

例如,如果我有 30 个项目,这些项目中的每一个都将生成一个新的组合框,因此名称将由 asp 生成。我需要的是能够从用户正在使用的任何组合框中获取选定的值。我目前正在使用 jQuery 和一些荒谬的parent().parent().children()类型废话,以便找到与提交按钮相关的正确组合框。

When submit button is clicked, I need it to find the selected value of the it's respective combobox so that I can send that to the post submission handler. The problem is that the .val()jQuery method is not working. When I use that with something like:

单击提交按钮时,我需要它找到其各自组合框的选定值,以便我可以将其发送到提交后处理程序。问题是.val()jQuery 方法不起作用。当我将其用于以下内容时:

$(this).parent().parent().children().children(".statusCbo").val();

I end up getting the text value, not the selected value. I triple checked to make sure that I had the fields bound correctly in the aspx file;

我最终得到了文本值,而不是选定的值。我三重检查以确保我在 aspx 文件中正确绑定了字段;

DataTextField = '<%#Eval("name") %>' DataValueField = '<%#Eval("id") %>'

DataTextField = '<%#Eval("name") %>' DataValueField = '<%#Eval("id") %>'

But as I said, I'm ending up with the DataTextField value of the selected item. The best explanation I could get was that it had something to do with how the control is requesting the content (via ajax).

但正如我所说,我最终得到了所选项目的 DataTextField 值。我能得到的最好解释是它与控件如何请求内容(通过 ajax)有关。

So at any rate, could anyone offer some suggestions on how to accurately get the selected value from the combobox?

因此,无论如何,有人可以就如何准确地从组合框中获取所选值提供一些建议吗?

UPDATE: I was able to gain reference to the object through a different means:

更新:我能够通过不同的方式获得对对象的引用:

$(".submitTag").click(
        function () {
            var topLevel = $(this).closest(".CommentTopLevel");
            var status = topLevel.find(".StatusTag").get_value();                   

            //stub to test value
            alert(status);
            return false;
        });

from here, if use status.val(), it will give me the text instead of the value (same issue as before). The documentation implies that I should use status.get_value(); but this is blowing up saying that the method is not supported from the object. Any ideas?

从这里开始,如果使用 status.val(),它会给我文本而不是值(与以前相同的问题)。文档暗示我应该使用 status.get_value(); 但这是爆炸性的说对象不支持该方法。有任何想法吗?

UPDATE: nevermind, I found that it is a jquery object being returned, so the method isn't included. Continuing to dig.

更新:没关系,我发现它是一个被返回的 jquery 对象,所以不包括该方法。继续挖。

SOLUTION: There was just an extra step i needed to do to use traditional methods. I don't know what it took so long for it to click with me:

解决方案:我需要做一个额外的步骤来使用传统方法。我不知道它花了这么长时间才跟我一起点击:

$(".submitTag").click(
    function(){
        var topLevel = $(this).closest(".CommentTopLevelTag"); //gets the parent container
        var comboBoxID = topLevel.find(".StatusTag").attr("ID"); //gets the clientID of the jQuery object
        var comboBoxRef = $find(comboBoxID); //finds the control by id and brings back a non-jQuery object (useable)
        var comboBoxSelectedValue = comboBoxRef.get_value(); //uses the Telerik method to get the selected value
    });

采纳答案by JayC

Its been a little while since I've dealt with Telerik controls, but what you're doing is bypassing the apis Telerik has made for you to use, and that strikes me as a very bad thing. The next release of Telerik Controls could easily break your code.

自从我处理 Telerik 控件以来已经有一段时间了,但是您正在做的是绕过 Telerik 为您提供的 api,这让我觉得这是一件非常糟糕的事情。Telerik Controls 的下一个版本很容易破坏您的代码。

Look, it shouldn't be that hard to pass the client id from the listview. There's several methods I'd tackle but I'll let you figure that on your own for now. Once you DO have the ClientID for the control, follow the example on telerik's site:

看,从列表视图传递客户端 ID 应该不难。有几种方法我会解决,但我现在让你自己解决。获得控件的 ClientID 后,请按照 Telerik 站点上的示例进行操作:

http://demos.telerik.com/aspnet-ajax/combobox/examples/programming/addremovedisableitemsclientside/defaultcs.aspx

http://demos.telerik.com/aspnet-ajax/combobox/examples/programming/addremovedisableitemsclientside/defaultcs.aspx

Once you have that id do some

一旦你有那个 id 做一些

var combo = $find(someVarReferencingTheId); 

Now you have a reference to the combobox in its clientside form. Now find some function that gets what you want from here:

现在,您可以引用客户端形式的组合框。现在找到一些可以从这里得到你想要的函数:

http://www.telerik.com/help/aspnet-ajax/combobox-client-side-radcombobox.html

http://www.telerik.com/help/aspnet-ajax/combobox-client-side-radcombobox.html

...

...

PROFIT!

利润!

EDIT: that first link to demos.telerik.com isn't really even needed, I just showed that because that's what I used to get that line of code (I could never remember if it's $getor $findI needed to use, unless I was doing a lot of Telerik clientside stuff at the time.).

编辑:到 demos.telerik.com 的第一个链接实际上甚至不需要,我只是展示了这一点,因为那是我用来获取那行代码的内容(我永远不记得它是否$get$find我需要使用,除非我正在做当时有很多 Telerik 客户端的东西。)。

EDIT 2: $getand $findare ASP.NET constructs, not Telerik's.

编辑 2: $get并且$find是 ASP.NET 构造,而不是 Telerik 的。