2016-09-21 4 views
0

ため、このJavaScriptコードのためのいくつかの助けが必要私のコードです:ここに日付ピッカー

$(function() { 
 
    $('#startDate').datepicker({ 
 
    dateFormat: "dd/mm/yy", 
 
    changeMonth: true, 
 
    changeYear: true, 
 
    showButtonPanel: true, 
 
    onClose: function(dateText, inst) { 
 

 

 
     function isDonePressed() { 
 
     return ($('#ui-datepicker-div').html().indexOf('ui-datepicker-close ui-state-default ui-priority-primary ui-corner-all ui-state-hover') > -1); 
 
     } 
 

 
     if (isDonePressed()) { 
 
     var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val(); 
 
     var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val(); 
 
     $(this).datepicker('setDate', new Date(year, month, 1)).trigger('change'); 
 

 
     $('.date-picker').focusout() 
 
     } 
 
    }, 
 
    beforeShow: function(input, inst) { 
 

 
     inst.dpDiv.addClass('month_year_datepicker') 
 

 
     if ((datestr = $(this).val()).length > 0) { 
 
     year = datestr.substring(datestr.length - 4, datestr.length); 
 
     month = datestr.substring(0, 2); 
 
     $(this).datepicker('option', 'defaultDate', new Date(year, month - 1, 1)); 
 
     $(this).datepicker('setDate', new Date(year, month - 1, 1)); 
 
     $(".ui-datepicker-calendar").hide(); 
 
     } 
 
    } 
 
    }) 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.0/themes/smoothness/jquery-ui.css"> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.0/jquery-ui.min.js"></script> 
 
<input id="startDate" />

問題は、私は私の年の最初の日になぜ日付の変更を知りませんでフォーカスアウトして戻ってくる。コードでエラーが見つかった場合は、教えてください。

jsfiddle

+0

に補正

month = datestr.substring(0, 2); 

たヘルプ@deppermに対するエラー感謝を見つけます – depperm

+0

'$(this).datepicker( 'setDate'、new Date(year、month-1、1));' beforeShowの行がこれを引き起こす原因です。 – ashok93

+0

putアラート(datestr);アラート(year);アラート(月);適切な場所にあなたが表示されます –

答えて

0

私はそれがこの部分にあなたがして、日付を `beforeShow`の一部を削除する場合

month = datestr.substring(3, 5);