开发者

How to not get datepicker to clear text field values?

I have a website that looks like

How to not get datepicker to clear text field values?

where "Begin Date" and "End Date" are datepicker.

When generating the webpage I can fill in values in those fields, but adding datepicker to them as well, clears the values.

I initialize datepicker like so

$(function() {
    var dates = $("#from, #to").datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        numberO开发者_如何学GofMonths: 3,
        onSelect: function(selectedDate) {
            var option = this.id == "from" ? "minDate" : "maxDate",
            instance = $(this).data("datepicker"),
            date = $.datepicker.parseDate(
                instance.settings.dateFormat ||
                $.datepicker._defaults.dateFormat,
                selectedDate, instance.settings);
            dates.not(this).datepicker("option", option, date);
        }
    });


    $("#from, #to").datepicker("option", "dateFormat", "dd/mm-yy");
});

and the HTML looks like

<input class="new" type="text" id="from" name="from"/>

How do I get datepicker to not clear the form field values when the webpage is loaded?


Don't know exactly why but it has to do with your onSelect function. I think you're expecting a special dateFormat and, given that your initial value doesn't have that format, datepicker just drops the value.

If you, however, set the dateFormat initially matching the initial value of your input, you won't have any problem.

Working example: http://jsfiddle.net/marcosfromero/3MG9D/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜