开发者

get text vaule of dropdownlist client side

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_techrev开发者_运维技巧iew.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!


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


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 ');");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜