Separate a jQuery UI datepicker into 3 separate text boxes

夙愿已清 提交于 2019-12-06 05:39:45

问题


Does anyone know of an elegant way of separating the jquery-ui datepicker's input into 3 separate boxes?

By default its input is just one textbox: DD/MM/YYYY

I have to separate the input into 3 separate ones for day, month, and year: DD, MM, and YYYY.


回答1:


<input type='text' id='fullDate' name='fulldate'>
<!-- Your other HTML fields listed above -->

$('#fullDate').datepicker({
    onSelect: function(dateText, inst) {
        var pieces = dateText.split('/');
        $('#day').val(pieces[0]);
        $('#month').val(pieces[1]);
        $('#year').val(pieces[2]);
    }
})

working example http://jsfiddle.net/jomanlk/7NgpQ/




回答2:


You can use the datepicker function onClose then grab the value.

See this http://jsfiddle.net/96FPU/

This will only work if the dateFormat is correctly referenced by the array index, i.e. in my example, it is working from day = index 0, month = index 1, year = index 1. Also in my example i am just assigning the values to spans'




回答3:


You can use the .datepicker('dialog') method to achieve this effect.

function onSelect(dateText, inst) {
    var pieces = dateText.split('/');
    for (var i = 0; i < pieces.length; ++i)
        $('.date:eq('+i+')').val(pieces[i]);
};

$('.date').click(function(event) {
    var d = $('.date').map(function() { return $(this).val() }).get().join('/');
    $(this).datepicker('dialog', d, onSelect, { dateFormat: 'dd/mm/yy' }, [10, 40]);
});

See this in action: http://jsfiddle.net/william/QHasQ/.



来源:https://stackoverflow.com/questions/7659958/separate-a-jquery-ui-datepicker-into-3-separate-text-boxes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!