【问题标题】:jquery when checkbox is not checked hide a textbox未选中复选框时的jquery隐藏文本框
【发布时间】:2015-05-06 07:33:11
【问题描述】:

我有一个textbox 和一个checkbox,当checkbox 未选中时,我需要禁用textbox,当用户选择checkbox 时,我想重新启用@987654329 @。

我试过这个:

ASP.NET 代码:

<div class="checkConfiguration">
    <asp:CheckBox runat="server" ID="stopGeneralNumber" Text="General Number"   CssClass="cbStopReason" Checked="false" />
    <asp:TextBox runat="server" Enabled="false"></asp:TextBox>
</div>

jQuery 代码

 $('.cbStopReason').on('click', function () {
        if ($(this).attr('checked')) {
            alert("now checked");
            $(this).nextAll("input").removeAttr("disabled");
        } else {
            alert("now un checked");
            $(this).nextAll("input").attr("disabled", "disabled"); 
        }
    })

jQuery 代码已经在document ready function 中,但问题是我的代码可以很好地禁用textbox,但重新启用它却不起作用,请问我做错了什么?

更新 1:这是正在生成的实际 HTML

 <div class="configurationData">
                            <div class="checkConfiguration">
                                <span class="cbStopReason"><input id="stopUrgentNumber" type="checkbox" name="stopUrgentNumber"><label for="stopUrgentNumber">Urgent Number</label></span>
                                <input name="ctl17" type="text" disabled="disabled" class="aspNetDisabled">
                            </div>
                            <div class="checkConfiguration">
                                <span class="cbStopReason"><input id="stopNurseNurse" type="checkbox" name="stopNurseNurse"><label for="stopNurseNurse">Nurse Number</label></span>
                                <input name="ctl18" type="text" disabled="disabled" class="aspNetDisabled">
                            </div>
                            <div class="checkConfiguration">
                                <span class="cbStopReason"><input id="stopScheduledNumber" type="checkbox" name="stopScheduledNumber"><label for="stopScheduledNumber">Scheduled Number</label></span>
                                <input name="ctl19" type="text" disabled="disabled" class="aspNetDisabled">
                            </div>
                            <div class="checkConfiguration">
                                <span class="cbStopReason"><input id="stopClockNumber" type="checkbox" name="stopClockNumber"><label for="stopClockNumber">Clock Number</label></span>
                                <input name="ctl20" type="text" disabled="disabled" class="aspNetDisabled">
                            </div>
                            <div class="checkConfiguration">
                                <span class="cbStopReason"><input id="stopLastCheckNumber" type="checkbox" name="stopLastCheckNumber"><label for="stopLastCheckNumber">Last Check Number</label></span>
                                <input name="ctl21" type="text" disabled="disabled" class="aspNetDisabled">
                            </div>
                            <div class="checkConfiguration">
                                <span class="cbStopReason"><input id="stopArrivalNumber" type="checkbox" name="stopArrivalNumber"><label for="stopArrivalNumber">Arrival Number</label></span>
                                <input name="ctl22" type="text" disabled="disabled" class="aspNetDisabled">
                            </div>
                            <div class="checkConfiguration">
                                <span class="cbStopReason"><input id="stopGeneralNumber" type="checkbox" name="stopGeneralNumber"><label for="stopGeneralNumber">General Number</label></span>
                                <input name="ctl23" type="text" disabled="disabled" class="aspNetDisabled">
                            </div>
                        </div>

【问题讨论】:

标签: javascript jquery asp.net checkbox


【解决方案1】:

根据您的 OP,它看起来像是您在跨度上的绑定。将其更改为绑定在您的复选框上。还将绑定从单击更改为更改。 http://jsfiddle.net/0fL0pumf/1/

$('#stopGeneralNumber').on('change', function () {
    var $this = $(this);

    if ($this.is(':checked')) {
        $this.parent().nextAll("input").prop("disabled", false);
    } else {
        $this.parent().nextAll("input").prop("disabled", true);
    }
});

减少了 javascript...

$('#stopGeneralNumber').on('change', function () {
    $(this).parent().nextAll("input").prop("disabled", !$(this).is(':checked'));
});

使用类的泛型,但不使用复选框的 id。只需使用不同的选择器。

$('.cbStopReason > input[type=checkbox]').on('change', function () {
    var $this = $(this);
    $this.parent().nextAll("input").prop("disabled", !$this.is(':checked'));
});

【讨论】:

  • 我不能使用id,我必须使用class,因为我有太多复选框,我想使用class来处理它们,我刚刚更新了我的问题给你我拥有的五个复选框
  • 我试过你的代码,但我仍然面临同样的问题,我在想可能是因为你只在一个复选框上尝试了你的代码,而我有五个,我更新了问题给你我生成的html
【解决方案2】:

在呈现的代码中,您不再有一个带有类 cbStopReason 的复选框——该类应用于 span,并且范围永远不会是 checked

一种解决方法是将条件更改为:

$(this).find("input").attr('checked')

【讨论】:

  • 你能给我一个工作的jsfiddle吗?或者至少告诉我我的代码在真假条件下会是什么样子
  • jsfiddle.net/9vLtpqtt/5 只需在您的if 中的.attr 之前添加.find("input")
  • 你放的代码在我的机器上不起作用,虽然它在 jsfiddle 上运行良好,但我找不到问题,我给了你代码,它如何在 jsffidle 上运行,但在我的机器上不行机器好吗?
  • 我尝试在if 声明之前执行此alert($(this).find("input").attr('checked'));,但我一直在打印undifinied
【解决方案3】:

您将事件处理程序绑定到错误的元素(选择器)。此外,您可以大大减少代码。

我会这样做:

$(function () {
    // On the checkbox click.
    // Here I would give these checkboxes a particular class
    // to use on the selector.
    $('.checkConfiguration input[type=checkbox]').on('click', function () {
        // Go to its parent and look for the input.
        // Enable/disable it according to the checkbox 
        // checked value.
        $(this).closest('.checkConfiguration')
            .find('input[type=text]')
            .prop('disabled', !$(this).is(':checked'));
    });
});

Demo

【讨论】:

    【解决方案4】:

    请改用if ($(this).is(':checked'))

    【讨论】:

    • 我在这里问问题之前已经这样做了,但我仍然遇到问题中描述的相同问题
    • 但是当您激活复选框时浏览器会显示警报吗?
    • 警告总是写在false部分
    猜你喜欢
    • 1970-01-01
    • 2011-03-19
    • 2016-03-28
    • 2013-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多