jQuery how to split today's date from input into month, day, year when datepicker NOT used

落爺英雄遲暮 提交于 2019-12-13 02:48:15

问题


I have a datepicker input that is populated with today's date when the page loads. When datepicker is used, the date is split into month, day, and year and put into three hidden fields that are sent to the sever with the post data. The problem comes when the user decides today's date is fine and doesn't use the datepicker; how do I get the date pieces into those three hidden fields?

I tried breaking out today's date into pieces and adding it to those fields but every way I've tried to add it into jQuery or using plain JavaScript breaks the whole thing. Here is the working code that I'd like to build on:

jQuery

$(function() {
    //set your date picker
        $("#CI").datepicker({
        dateFormat: 'mm/dd/yy',
        changeMonth: true,
        changeYear: true,
        showOn: "both",
        buttonImage: "css/images/calendar-green.gif",
        buttonImageOnly: true,
        buttonImageText: "Calendar",
        beforeShow: function(dateText, instance) { 
            $("#CI").datepicker('setDate', new Date());
        },
        onSelect: function(dateText, instance) {
            var pieces = dateText.split("/");
             $("#CID").val(pieces[0]);
             $("#CIM").val(pieces[1]);
             $("#CIY").val(pieces[2]);
         }                
    })

    //get the current date
    var todaysDate = new Date();
    //format the date in the right format (add 1 to month because JavaScript counts from 0)
    formatDate = (todaysDate.getMonth() + 1) + '/' + 
                         todaysDate.getDate() + '/' + 
                         todaysDate.getFullYear();
    //set the input value
    $('#CI').val(formatDate);       
});

HTML

<input type='text' id='CI' name="CI">
<input type="text" name="CID" id="CID" />
<input type="text" name="CIM" id="CIM" />
<input type="text" name="CIY" id="CIY" />

回答1:


Split out your onSelect() functionality into a separate function, which can them be called twice.

Having the same function performing both tasks allows you to deal with any changes to your functionality or HTML structure.

so...

$(function() {
    function populateHidden(dateText){
        var pieces = dateText.split("/");
        $("#CID").val(pieces[0]);
        $("#CIM").val(pieces[1]);
        $("#CIY").val(pieces[2]);
    }
    //set your date picker
    $("#CI").datepicker({
        dateFormat: 'mm/dd/yy',
        changeMonth: true,
        changeYear: true,
        showOn: "both",
        buttonImage: "css/images/calendar-green.gif",
        buttonImageOnly: true,
        buttonImageText: "Calendar",
        beforeShow: function(dateText, instance) { 
            $("#CI").datepicker('setDate', new Date());
        },
        onSelect: populateHidden
    })

    //get the current date
    var todaysDate = new Date();
    //format the date in the right format (add 1 to month because JavaScript counts from 0)
    formatDate = (todaysDate.getMonth() + 1) + '/' + 
                         todaysDate.getDate() + '/' + 
                         todaysDate.getFullYear();
    //set the input value
    $('#CI').val(formatDate); 
    populateHidden(formatDate); 
});



回答2:


Use the following code:

   //get the current date
    var todaysDate = new Date();
    //format the date in the right format (add 1 to month because JavaScript counts from 0)
    var month = todaysDate.getMonth() + 1 ;
    var date = todaysDate.getDate();
    var year =todaysDate.getFullYear() ;

    formatDate = month + '/' +
                   date+ '/' +
                   year;
    //set the input value
    $('#CI').val(formatDate);       

    $("#CID").val(date);
    $("#CIM").val(month);
    $("#CIY").val(year);


来源:https://stackoverflow.com/questions/6223207/jquery-how-to-split-todays-date-from-input-into-month-day-year-when-datepicke

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