how to work with javascript function for asp.net checkbox control which is placed in contentplaceholder?

StackOverflow https://stackoverflow.com/questions/8296130

문제

I have checkbox and textbox control from asp.net on the contentplaceholder. I want to call the javascript function for enabling the textbox control depending on the checked status of the checkbox. I have written following javascript for this-

name of the checkbox & textbox respectively after rendering it to browser ctl00$ContentPlaceHolder1$chkCall, ctl00$ContentPlaceHolder1$txtCall

function chkChanged() {
           try {

           var echk = document.getElementsByName('ctl00$ContentPlaceHolder1$chkCall');
           var etxt = document.getElementsByName('ctl00$ContentPlaceHolder1$txtCall');

           if (echk.Checked) {

               etxt.Enabled = true;
           }
           else {
               etxt.Enabled = false;
               etxt.Text = "";
           }
           return true;
           }
       catch (err) {
           alert(err.Message);
           return false;
       }
       }

When i'm executing above script then it calls function but the code is not working for the control. And it is not throwing any exception.

What is wrong in above script?

thanks.

도움이 되었습니까?

해결책

Use this script instead:

var echk = document.getElementById("<%= chkCall.ClientID %>");
var etxt = document.getElementById("<%= txtCall.ClientID %>");

다른 팁

getElementsByName returns an array of elements (plural) try using getElementById to get a single one element, which is probably what you want because you are checking whether echk.Checked == true. In your case echk is an array, not a checkbox.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top