将minDate设置为jQuery datapicker中的动态日期

时间:2015-08-24 17:53:06

标签: javascript jquery-ui jquery-ui-datepicker

jQuery-UI datepicker中的最小日期设置(minDate)应该设置为birthdate(我的代码中的birthdateDMY)。我在考虑在.datepicker({})中使用匿名函数,但我不知道如何使其工作。

//birthdateDMY will be something like [ "01", "07", "2015" ]
var name = jQuery("#dropdown").val();
var index = baby.Name.indexOf(name);
var birthdateDMY = baby.BirthDate[index].split("/");

jQuery(function() {   
    jQuery("#datepicker").datepicker({
        //need an anonymous function here to set minDate?
        minDate: (new Date(birthdateDMY[2], birthdateDMY[1] - 1,
birthdateDMY[0])),
    maxDate: 0, 
    dateFormat: "dd/mm/yy"
});

使用SETTER解决:

//Setter: update minDate for e.g. #datepicker
function updateMinDate(selector){
    //birthdateDMY will be something like [ "01", "07", "2015" ]
    var name = jQuery("#dropdown").val();
    var index = baby.Name.indexOf(name);
    var birthdateDMY = baby.BirthDate[index].split("/");
    //Update minDate in the selected item
    $(selector).datepicker("option", "minDate", new Date(birthdateDMY[2], birthdateDMY[1] - 1, birthdateDMY[0]) );
}

//When changin #dropdown, call updateMinDate
jQuery(document).on('change', '#dropdown', function(e) {
    updateMinDate("#datepicker");
}



jQuery(function() {
  jQuery("#datepicker").datepicker({
    minDate: <some code run when page loads>,
    maxDate: 0, numberOfMonths: 2, dateFormat: "dd/mm/yy"
  });
});

1 个答案:

答案 0 :(得分:0)

每当下拉列表更改时,您都需要重新初始化日期选择器。现在,当您的页面加载时,它会设置日期选择器,然后它永远不会更改它。 所以,这样的事件应该会有所帮助:

$(document).ready(function () {

    $.fn.dataTableExt.ofnSearch["html-input"] = function (value) {
        return $(value).val();
    };

    var table = $("#lines").DataTable({
        "searching": true,
        "ordering": true,
        columnDefs: [
            { "targets": [15, 16], "searchable": false, "orderable": false },
            { "type": "html-input", "targets": [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14] }
        ],

    });

    $("#lines td input").on("change", function () {
        var td = $(this).parent();

        console.log($(this).parent().find("input").val());
        table.row($(this).closest("tr")).invalidate();
    });

    // Apply the search
    $("#strength_search").on("change", function () {
        table
            .columns(6)
            .search(this.value)
            .draw();
    });
    $("#dose_search").on("change", function () {
        table
            .columns(7)
            .search(this.value)
            .draw();
    });

});

您可能需要稍微玩一下,但我认为这可以解决您的问题。