获取下拉列表客户端的文本值

发布于 2024-09-17 05:10:37 字数 1303 浏览 5 评论 0原文

我正在尝试将客户端事件添加到下拉列表中,并且需要访问当前选定的文本。我已经尝试过:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

辞取 2024-09-24 05:10:37

JavaScript 中的字符串上没有 .Equals(),而是使用 === 运算符,如下所示:

ddl_tech.Attributes.Add("onchange", "document.getElementById('chk_techreview').disabled = ( this.options[this.selectedIndex].text === 'UNASSIGNED');");

您可以在这里尝试一下,我还将事件更改为 onchange 因为这可能更多你所追求的。此外,根据您的选择,它可能只是 'UNASSIGNED' 而不是 'UNASSIGNED'

There is no .Equals() on a string in JavaScript, instead use the === operator, like this:

ddl_tech.Attributes.Add("onchange", "document.getElementById('chk_techreview').disabled = ( this.options[this.selectedIndex].text === 'UNASSIGNED');");

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'.

黑白记忆 2024-09-24 05:10:37

我认为应该是:

ddl_tech.Attributes.Add("onclick", "var s = document.getElementById('" + chk_techreview.ClientID + "'); s.disabled = (s.selectedIndex == -1 || s.options[s.selectedIndex].text == ' UNASSIGNED ');");

I believe it should be:

ddl_tech.Attributes.Add("onclick", "var s = document.getElementById('" + chk_techreview.ClientID + "'); s.disabled = (s.selectedIndex == -1 || s.options[s.selectedIndex].text == ' UNASSIGNED ');");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文