1

Here is my code:

$("#myCheckBox").click(function () {
        if (!$(this).is(':checked')) {
            $('#txtDateFrom').attr('readonly', true);
            $('#txtDateTo').attr('readonly', true);
            $('#txtDateFrom').val('01/01/2012');
            $('#txtDateTo').val('31/12/2012');
            $('#txtDateFrom').datepicker(this.checked ? "disable" : "disable");
            $('#txtDateTo').datepicker(this.checked ? "disable" : "disable");
        }
        else {
            $('#txtDateFrom').attr('readonly', false);
            $('#txtDateTo').attr('readonly', false);
            $('#txtDateFrom').datepicker(this.checked ? "disable" : "enable");
            $('#txtDateTo').datepicker(this.checked ? "disable" : "enable");
            $('#txtDateFrom').datepicker({ dateFormat: 'dd/mm/yy' });
            $('#txtDateTo').datepicker({ dateFormat: 'dd/mm/yy' });
        }
    });

After uncheking the checkBox, and checking it again, the textboxes are still disabled. Why?

4

2 回答 2

3

这可能是你的问题:

$('#txtDateFrom').datepicker(this.checked ? "disable" : "disable");
$('#txtDateTo').datepicker(this.checked ? "disable" : "disable");

应该是这样的:

$('#txtDateFrom').datepicker(this.checked ? "disable" : "enable");
$('#txtDateTo').datepicker(this.checked ? "disable" : "enable");
于 2012-04-04T10:54:33.650 回答
1

尝试.removeAttr()通过将 readonly 设置为 false 而不是.attr(). 例如:

$('#txtDateFrom').removeAttr('readonly');
于 2012-04-04T10:54:28.167 回答