2011-09-22 6 views
0

선택하면 2 개의 텍스트 상자가 비활성화되는 체크 박스가 있습니다. 선택을 취소하면 체크 박스가 활성화됩니다. 여기에 자바 스크립트는 다음과 같습니다변경시 텍스트 상자 사용 안 함 변경됨

<dl> 
    <dt><label for="CheckBox1">PreAnalytical?</label></dt> 
    <dd> <asp:CheckBox ID="CheckBox1" runat="server" CausesValidation="false" 
          Visible="true" OnCheckChanged="enableField()"/></dd> 
</dl> 
<dl> 
    <dt><label for="prePracticeCodeTextBox">Practice Code:</label></dt> 
    <dd><asp:TextBox ID="prePracticeCodeTextBox" runat="server" Enabled="False" /></dd> 
</dl> 
<dl> 
    <dt><label for="preContactTextBox">Contact:</label></dt> 
    <dd><asp:TextBox ID="preContactTextBox" runat="server" Enabled="False" /></dd> 
</dl> 

은 자바 스크립트 기능이 전혀 호출되지 않는 : 여기

function enableField() { 
    prePracticeCodeTextBox = document.getElementById('prePracticeCodeTextBox'); 
    preContactTextBox = document.getElementById('preContactTextBox'); 
    checkTheBox = document.getElementById('CheckBox1'); 
    if (checkTheBox.checked == true) { 
     prePracticeCodeTextBox.disabled = false; 
     preContactTextBox.disabled = false; 

    } 
    else { 
     prePracticeCodeTextBox.disabled = true; 
     preContactTextBox.disabled = true; 
    } 
} 

는 HTML입니다.

내가 뭘 잘못하고 있니?

답변

3

대신 onclick을 사용해보세요. 뒤에 코드를 등록하려면 다음 코드를 사용

CheckBox1.Attributes.Add("onclick", "enableField();"); 

BTW, 당신은 당신이 기본 설정으로 asp.net 웹 양식 응용 프로그램에서와 마찬가지로 요소에 도달 할 수 없습니다. 당신은 렌더링되는 요소의 ClientIDs 얻을 필요가 :

http://www.tugberkugurlu.com/archive/we-lovenet-4-clean-web-control-ids-with-clientidmode-property-to-static-and-predictable

+0

<% = ... 사용하지 않아도됩니다. –

+1

그것은 응용 프로그램이 깨끗한 ID를 생성하도록 구성되었음을 의미합니다. 그것을 염두에 두십시오. ContentPlaceHolder 안에 코드를 넣으면 (마스터 페이지를 사용할 때) 요소의 ID가 확실히 바뀝니다. – tugberk

1

OnCheckChanged="enableField()에서 onclick="enableField();"으로 변경하십시오. 서버 컨트롤을 사용하고 있습니다. OnCheckChanged는 asp.net CheckBox 컨트롤의 이벤트입니다.

+0

정말 고마워요 !!!!!!!!!!!!!!!!!!!!!!!!!!!!! –

0

기능 enableField() {

:

function enableField() { 
    prePracticeCodeTextBox = document.getElementById('<%=prePracticeCodeTextBox.ClientID%>'); 
    preContactTextBox = document.getElementById('<%=preContactTextBox.ClientID%>'); 
    checkTheBox = document.getElementById('<%=CheckBox1.ClientID%>'); 
    if (checkTheBox.checked == true) { 
     prePracticeCodeTextBox.disabled = false; 
     preContactTextBox.disabled = false; 

    } 
    else { 
     prePracticeCodeTextBox.disabled = true; 
     preContactTextBox.disabled = true; 
    } 


} 

는 .NET 4에서 개발하는 경우를, 아래 기사를 읽고

prePracticeCodeTextBox =.getElementById('<%=prePracticeCodeTextBox.ClientID%>'); 
preContactTextBox = document.getElementById('<%=preContactTextBox.ClientID%>'); 
checkTheBox = document.getElementById('<%=CheckBox1.ClientID%>'); 

if (checkTheBox.checked == true) { 

    prePracticeCodeTextBox.disabled = false; 
    preContactTextBox.disabled = false; 

} 
else { 

    prePracticeCodeTextBox.disabled = true; 
    preContactTextBox.disabled = true; 
} 

}