是否可以在 Javascript 中的 Telerik RadDropDownList 中设置选定的值?

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

Is it possible to set the selected value in a Telerik RadDropDownList in Javascript?

javascriptasp.nettelerik

提问by user2320724

Is it possible to set the selected value/item in a Telerik RadDropDownList in Javascript?

是否可以在 Javascript 中的 Telerik RadDropDownList 中设置选定的值/项目?

I'm looking at the documentation http://www.telerik.com/help/aspnet-ajax/dropdownlist-clientsideprogramming-clientsidebasic.htmland it seems there is no documented way to do this simple and essential task.

我正在查看文档http://www.telerik.com/help/aspnet-ajax/dropdownlist-clientsideprogramming-clientsidebasic.html似乎没有记录的方法来完成这个简单而重要的任务。

Is it simply not in the documentation? Tried searching the forums as well to no avail.

它只是不在文档中吗?也试过搜索论坛也无济于事。

回答by Win

You can use select()or set_selected(true)function. Hereis the documentation.

您可以使用select()set_selected(true)函数。是文档。

<telerik:RadDropDownList ID="RadDropDownList1" runat="server">
    <Items>
        <telerik:DropDownListItem runat="server" Text="One" Value="1" />
        <telerik:DropDownListItem runat="server" Text="Two" Value="2" />
        <telerik:DropDownListItem runat="server" Text="Three" Value="3" />
    </Items>
</telerik:RadDropDownList>
<script type="text/javascript">
    function pageLoad() {
        var dropdownlist = $find("<%= RadDropDownList1.ClientID %>");
        var item = dropdownlist.findItemByValue("3");
        item.select();
    }
</script>

回答by Pierre Contri

var dropdownlist = document.getElementById('<%= RadDropDownList1.ClientID %>');
if (dropdownlist != null) {
    dropdownlist .children[0].children[0].innerHTML = "3 - Low";
    dropdownlist .children[0].children[0].setAttribute("class", "rddlFakeInput");
}

No other solutions worked on my project !! 3 days for the item.selected = true; it's not acceptable, so JavaScript in 20 minutes

没有其他解决方案适用于我的项目!!item.selected = true 3 天;这是不可接受的,所以 JavaScript 在 20 分钟内