簡體   English   中英

如果在中繼器中未選中復選框,則禁用文本框

[英]Disable textbox when checkbox is not checked inside a repeater

在我的.aspx中,Repeater內部包含以下元素:

<asp:CheckBox runat="server" ID="chkNormalServiceCost" Checked="true" OnClick="EnableDisableTextBox()"/>
<asp:TextBox runat="server" ID="txtNormalServiceCost" />

<script>標記中有jQuery函數:

function EnableDisableTextBox() {
    $('#chkNormalServiceCost').change(function () {
        if ($('#chkNormalServiceCost').is(':checked') == true)
            $('#txtNormalServiceCost').prop('disabled', false);
        else
            $('#txtNormalServiceCost').prop('disabled', true);
    });
}

但仍然無法正常工作。

這是因為控件的id屬性在呈現時會自動生成; 它在客戶端不會是chkNormalServiceCost 您需要改為在其上設置ClientID屬性:

<asp:CheckBox runat="server" ID="chkNormalServiceCost" ClientID="chkNormalServiceCost" Checked="true" />
<asp:TextBox runat="server" ID="txtNormalServiceCost" ClientID="txtNormalServiceCost" />

然后,您可以從那里使用jQuery添加單擊處理程序,而不是在觸發第一次click后附加change事件:

$('#chkNormalServiceCost').change(function () {
    $('#txtNormalServiceCost').prop('disabled', !this.checked);
});

小提琴的例子

嘗試這個

function EnableDisableTextBox() {
     if ($('#<%= chkNormalServiceCost.ClientID%>').is(':checked') == true)
         $('#<%= txtNormalServiceCost.ClientID%>').prop('disabled', false);
     else
         $('#<%= txtNormalServiceCost.ClientID%>').prop('disabled', true);
}

使用OnClientClick =“ EnableDisableTextBox()”代替OnClick

<asp:CheckBox runat="server" ID="chkNormalServiceCost" Checked="true" OnClientClick="EnableDisableTextBox()"/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM