开发者

Jquery split textfield value and update in other fields on change

I have a text input field with jquery datepicker which will set the value in format yyyy-mm-dd. When this field is updated with datepicker, I would need three other text fields later in the document updated based on this.

I have tried something like this开发者_运维技巧, with no luck:

$('input#date').change(function() {
    var date = $('input#date').text();

    var year = date.split('-')[0];
    var month = date.split('-')[1];
    var day = date.split('-')[2];

    $(".year").text(year);
    $(".month").text(month);
    $(".day").text(day);

});


Use .val() for inputs, not .text():

Code:

$( function()
{
    $( '#date' )
        .change( function()
        {
            var dateParts = $( this ).val().split('-');

            $( '.year' ).val( dateParts[0] );
            $( '.month' ).val( dateParts[1] );
            $( '.day' ).val( dateParts[2] );
        } );
} );

Live Demo:

http://jsfiddle.net/JAAulde/6Z8yj/3/

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜