簡體   English   中英

Jquery電台和文本框

[英]Jquery Radio and text boxes

在我的Jsp中,我有兩個單選按鈕rb1和rb2以及兩個文本框txt1和txt2 ...

每當檢查rb2時,如何禁用文本框txt2?

這是我的代碼:

        <asp:TextBox runat="server" ID="txt1""></asp:TextBox>

        <asp:TextBox runat="server" ID="txt2"></asp:TextBox>

       <asp:RadioButton ID="rb1" Text="hello" runat="server" />

       <asp:RadioButton ID="rb2" Text="world" runat="server" />

您可以使用jQuery中的.change事件:

$("#<%= rb2.ClientID %>").change(function() {
    $("#<%= txt2.ClientID %>").prop("disabled", this.checked);
});

將它綁定到更改事件

$('[id*=rb2]').on('change', function() {
    var txt = $('[id*=txt2]');

    txt.prop('disabled', this.checked) : 
});

另外因為控件具有runa=server屬性,所以請確保使用屬性contains selector。

並且不要忘記將代碼包含在DOM就緒處理程序中。

最好在這里使用事件委托。

$(document).on('change', '[id^="rb"]', function() {
    var txt = $("#txt" + event.target.id.split(/rb/)[1]);

    txt.prop('disabled', this.checked) : 
});

只要任何單選按鈕發生變化,就會找到並禁用相應的輸入框。

暫無
暫無

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

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