javascript 从javascript设置RadComboBox值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11455388/
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
Set RadComboBox value from javascript
提问by nav100
I have a RadCombobox in a user control and I am trying to set the value to null or 0 from Javascript. The following code doesn't work. But it's not showing any error also.
我在用户控件中有一个 RadCombobox,我试图从 Javascript 中将该值设置为 null 或 0。以下代码不起作用。但它也没有显示任何错误。
function OnClientSelectedIndexChanged(sender, eventArgs) {
var item = eventArgs.get_item();
var ddl = document.getElementById('ctl00_plh1_Test1_Dropdown2_RadComboBox1_DropDown');
ddl.selectedIndex = 0;
}
<telerik:RadComboBox ID="Dropdown1" runat="server"
NoWrap="true" Width="250" OnClientSelectedIndexChanged="OnClientSelectedIndexChanged">
<CollapseAnimation Duration="200" Type="OutQuint" />
</telerik:RadComboBox>
<uc2:RadComboBox ID="Dropdown2" runat="server" DdlAutoWidth="true"></uc2:RadComboBox>
回答by codingbiz
Try this.
试试这个。
var mycombobox = $find("<%= MyUserControl.FindControl("RadComboBox1").ClientID %>");
or
或者
var mycombobox = $find("<%= RadComboBox1").ClientID %>");
mycombobox.clearSelection();
You might need this
你可能需要这个
<rad:RadScriptBlock runat="server" ID="RadCodeBlock">
<script type="text/javascript">
</script>
</rad:RadScriptBlock>
回答by Kevin Babcock
I assume the RadComboBox
that will trigger the OnClientSelectedIndexChanged
event will have 2 or more RadCombBoxItem
s, otherwise the event will never be fired as the selected index can never change.
我假设RadComboBox
将触发OnClientSelectedIndexChanged
事件的RadCombBoxItem
s将有 2 个或更多s,否则该事件将永远不会被触发,因为选定的索引永远不会改变。
When the event fires, you must get a reference to the RadComboBox
control inside your UserControl
. To set it's SelectedIndex property to 0 you call the set_selectedIndex()
function on the client-side control. Keep in mind that this only sets the SelectedIndex, and does not update the text in the input field of the RadComboBox
. If you want to clear that out as well you must call the client-side control's set_text()
function.
事件触发时,你一定要到一个参考RadComboBox
的内部控制UserControl
。要将其 SelectedIndex 属性设置为 0,您可以调用set_selectedIndex()
客户端控件上的函数。请记住,这只会设置 SelectedIndex,而不会更新RadComboBox
. 如果您也想清除它,您必须调用客户端控件的set_text()
函数。
function onComboBoxSelectedIndexChanged(s, e) {
var ctrl = '<%= Dropdown2.FindControl("RadComboBox1").ClientID %>';
if (ctrl) {
ctrl.set_selectedIndex(0);
ctrl.set_text('');
}
}
Please refer to the documentationon Telerik's website for more information regarding the JavaScript API for the RadCombBox control.
有关 RadCombBox 控件的 JavaScript API 的更多信息,请参阅Telerik 网站上的文档。
回答by user1560736
What do you want to set to 0
or null
the selected value or selected index?
Try this for the selected value:
您想设置什么0
或null
选定的值或选定的索引?
为选定的值试试这个:
$find("<%=ctl00_plh1_Test1_Dropdown2_RadComboBox1_DropDown.ClientID%>").set_text("0");
回答by Richard
Try:
尝试:
var radComboBox = <%=YourComboBox.ClientID %>;
radComboBox.SetValue("someValue");