获取下拉列表客户端的文本值
我正在尝试将客户端事件添加到下拉列表中,并且需要访问当前选定的文本。我已经尝试过:
ddl_tech.Attributes.Add("onclick", "document.getElementById('" + chk_techreview.ClientID + "').disabled = this.options[this.selectedIndex].text.Equals(' UNASSIGNED');");
并且
ddl_tech.Attributes.Add("onclick", "document.getElementById('" + chk_techreview.ClientID + "').disabled = this.text.Equals(' UNASSIGNED');");
当事件被触发时,这两个都会给我运行时错误。
客户端访问此文本属性的正确方法是什么?
我尝试了这个,但它没有启用复选框...
ddl_tech.Attributes.Add("onchange", "document.getElementById('" + chk_techreview.ClientID + "').disabled = this.options[this.selectedIndex].text == ' UNASSIGNED';");
答案:
好吧,当您设置 checkbox.enabled 时,必须使用 == 而不是 .Equals = false 在服务器端它会捕获标签中的复选框并将其设置为disabled=true;因此,您必须同时设置 checkbox.disabled = false 和 checkbox.parentElement.disabled = false;在客户端启用该复选框!
解决方法:
ddl_tech.Attributes.Add("onchange", "document.getElementById('" + chk_techreview.ClientID + "').parentElement.disabled = (this.options[this.selectedIndex].text == 'UNASSIGNED'); document.getElementById('" + chk_techreview.ClientID + "').disabled = (this.options[this.selectedIndex].text == 'UNASSIGNED');");
感谢您的帮助!
I am trying to add a client side event to a dropdownlist and I need to access the currently selected Text. I have tried:
ddl_tech.Attributes.Add("onclick", "document.getElementById('" + chk_techreview.ClientID + "').disabled = this.options[this.selectedIndex].text.Equals(' UNASSIGNED');");
and
ddl_tech.Attributes.Add("onclick", "document.getElementById('" + chk_techreview.ClientID + "').disabled = this.text.Equals(' UNASSIGNED');");
Both of which give me runtime errors when the event is fired.
Whats the correct way to access this text property client side?
I tried this but it does not enable the checkbox...
ddl_tech.Attributes.Add("onchange", "document.getElementById('" + chk_techreview.ClientID + "').disabled = this.options[this.selectedIndex].text == ' UNASSIGNED';");
ANSWER:
Well, along with having to use == rather than .Equals, when you set a checkbox.enabled = false on the server side it raps the checkbox in tags and sets it to disabled=true; therefore you must set BOTH the checkbox.disabled = false and checkbox.parentElement.disabled = false; on the client side to enable the checkbox!
The solution:
ddl_tech.Attributes.Add("onchange", "document.getElementById('" + chk_techreview.ClientID + "').parentElement.disabled = (this.options[this.selectedIndex].text == 'UNASSIGNED'); document.getElementById('" + chk_techreview.ClientID + "').disabled = (this.options[this.selectedIndex].text == 'UNASSIGNED');");
Thanks for the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
JavaScript 中的字符串上没有
.Equals()
,而是使用===
运算符,如下所示:您可以在这里尝试一下,我还将事件更改为
onchange
因为这可能更多你所追求的。此外,根据您的选择,它可能只是'UNASSIGNED'
而不是'UNASSIGNED'
。There is no
.Equals()
on a string in JavaScript, instead use the===
operator, like this:You can give it a try here, I also changed the event to
onchange
since that's probably more of what you're after. Also, depending on your option it may just be'UNASSIGNED'
rather than' UNASSIGNED'
.我认为应该是:
I believe it should be: