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/
精彩评论