我正在尝试使用jQuery锁定复选框更改事件,目前我有这个:

<script type="text/javascript">
    $(document).ready(function () {
        $('.timepicker').datetimepicker({ datepicker: false, format: 'H:i' });
        $('.mondaystartfinish').hide();

        //subscribe to change events
        $('#IsMonday').change(function () {
            RunsOnMondays();
        });
    });

    function RunsOnMondays() {
        if ($('#IsMonday').prop('checked') == 'true') {
            $('.mondaystartfinish').slideDown();
        } else {
            $('.mondaystartfinish').slideUp();
        }
    }
</script>

但是,在调试中,当我选中/取消选中该框时,RunsOnMondays()方法不会触发。

我也尝试过使用它,但仍然没有用:
$('IsMonday').prop('checked').change(function () { RunsOnMondays();});

浏览器输出
<div class="form-group">
    <label class="control-label col-md-2" for="IsMonday">Monday</label>
    <div class="col-md-10">
        <input class="check-box" data-val="true" data-val-required="The IsMonday field is required." id="IsMonday" name="IsMonday" type="checkbox" value="true" /><input name="IsMonday" type="hidden" value="false" />
        <span class="field-validation-valid" data-valmsg-for="IsMonday" data-valmsg-replace="true"></span>
    </div>
</div>

<div class="startfinish">
    <div class="form-group">
        <div class="mondaystartfinish">
            <label class="control-label col-md-2" for="MondayFrom">Monday Start</label>
            <div class="col-md-10">
                <input class="timepicker" data-val="true" data-val-date="The field MondayFrom must be a date." id="MondayFrom" name="MondayFrom" type="text" value="" />
                <span class="field-validation-valid" data-valmsg-for="MondayFrom" data-valmsg-replace="true"></span>
            </div>
        </div>
    </div>
</div>

<div class="startfinish">
    <div class="form-group">
        <div class="mondaystartfinish">
            <label class="control-label col-md-2" for="MondayTo">Monday Finish</label>
            <div class="col-md-10">
                <input class="timepicker" data-val="true" data-val-date="The field MondayTo must be a date." id="MondayTo" name="MondayTo" type="text" value="" />
                <span class="field-validation-valid" data-valmsg-for="MondayTo" data-valmsg-replace="true"></span>
            </div>
        </div>
    </div>
</div>

最佳答案

它不起作用的原因是,这不是正确的true == 'true'实际上是false:http://jsfiddle.net/TrueBlueAussie/2wwz1ore/6/
prop('checked')返回一个 bool 值!无需== 'true'

http://jsfiddle.net/TrueBlueAussie/2wwz1ore/1/

$(document).ready(function () {
    //$('.timepicker').datetimepicker({ datepicker: false, format: 'H:i' });
    $('.mondaystartfinish').hide();

    // subscribe to change events
    $('#IsMonday').change(function () {
        RunsOnMondays();
    });
});

function RunsOnMondays() {
    if ($('#IsMonday').prop('checked')) {
        $('.mondaystartfinish').slideDown();
    } else {
        $('.mondaystartfinish').slideUp();
    }
}

更好的是,使用slideToggle():http://jsfiddle.net/TrueBlueAussie/2wwz1ore/3/
    // subscribe to change events
    $('#IsMonday').change(function () {
        $('.mondaystartfinish').slideToggle($(this).prop('checked'));
    });

如果(我非常怀疑)您的元素是动态添加的,请使用附加到非更改祖先元素的委托(delegate)事件处理程序(如果没有其他更方便的方法,默认为document)

例如
    $(document).on('click' , '#IsMonday', function () {
        $('.mondaystartfinish').slideToggle($(this).prop('checked'));
    });

10-05 20:31