简体   繁体   English

Jquery电台和文本框

[英]Jquery Radio and text boxes

In my Jsp I have two radio buttons rb1 and rb2 and two text boxes txt1 and txt2... 在我的Jsp中,我有两个单选按钮rb1和rb2以及两个文本框txt1和txt2 ...

How can I disable my text box txt2 whenever rb2 was checked? 每当检查rb2时,如何禁用文本框txt2?

Here is my code: 这是我的代码:

        <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" />

You can use .change events from jQuery: 您可以使用jQuery中的.change事件:

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

Bind it to the change event 将它绑定到更改事件

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

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

Also because the controls have runa=server attributes, make sure you use the attribute contains selector. 另外因为控件具有runa=server属性,所以请确保使用属性contains selector。

And don not forget to include the code inside DOM ready handler. 并且不要忘记将代码包含在DOM就绪处理程序中。

It would probably be best to use event delegation here. 最好在这里使用事件委托。

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

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

Anytime any of the radio buttons change, the corresponding input box will be found and disabled. 只要任何单选按钮发生变化,就会找到并禁用相应的输入框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM