2017-08-30 14 views
0

日付ピッカーを使用して日付を入力する必要があるフィールドがあります。日付ピッカーを表示する方法

<table> 
<tr> 
<td style="width:150px;text-align:center; background-color:#f9f9f9";>From Date</td> 
<td style="background-color:#f9f9f9";><input style="width:150px;text-align:center" class="date-picker" name="oec_month1" ></td> 
<td style="width:150px;text-align:center; background-color:#f9f9f9";>To Date</td> 
<td style="background-color:#f9f9f9";><input style="width:150px;text-align:center" class="date-picker" name="oec_month2" ></td> 
</tr> 
</table> 

フィールドが表示できません:私はここでは、日付ピッカーのための私のスクリプト

<script> 
$(function(){ 
$('.date-picker').datepicker({ 
dateFormat: "yy-mm", 
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()//Added to remove focus from datepicker input box on selecting date 
    } 
}, 
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> 

は、HTMLでの私のフィールドである。ここで(表示しない)

を試してみましたが、エラーで検索しました日付選択ツール助けてください!

答えて

0

datepickerはjQueryの一部ではありません.jQueryUIにあります。 ライブラリを両方とも正しい順序で含める必要があります。あなたはそれをやった後は、あなたのコードは正常に動作します:

$(function() { 
 
    $('.date-picker').datepicker({ 
 
    dateFormat: "yy-mm", 
 
    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() //Added to remove focus from datepicker input box on selecting date 
 
     } 
 
    }, 
 
    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(); 
 
     } 
 
    } 
 
    }) 
 
});
td.label { 
 
    width: 150px; 
 
    text-align: center; 
 
    background-color: #f9f9f9; 
 
} 
 

 
td.field { 
 
    background-color: #f9f9f9; 
 
} 
 

 
.date-picker { 
 
    width: 150px; 
 
    text-align: center; 
 
}
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css"> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script> 
 

 
<table> 
 
    <tr> 
 
    <td class="label">From Date</td> 
 
    <td class="field"><input class="date-picker" name="oec_month1"></td> 
 
    <td class="label">To Date</td> 
 
    <td class="field"><input class="date-picker" name="oec_month2"></td> 
 
    </tr> 
 
</table>

はまた、私が代わりに醜い style属性を持つHTMLを梱包のスタイルシートを使用していることに注意してください。

+0

ありがとうを持っていることを確認してください。私はJqueryUI用のライブラリを含んでいます。間違ったライブラリが含まれているため、日付選択ツールが表示されません。私の質問に答えてくれてありがとう – NHH

+0

問題ありません。あなたがうまく働いてうれしい。それが助けられたら、答えを受け入れることを忘れないでください。 –

0

は、あなたが参照jquery-uiライブラリ

$(function() { 
 
    $('.date-picker').datepicker({ 
 
    dateFormat: "yy-mm", 
 
    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() //Added to remove focus from datepicker input box on selecting date 
 
     } 
 
    }, 
 
    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/1.11.0/jquery.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script> 
 
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css" /> 
 
<table> 
 
    <tr> 
 
    <td style="width:150px;text-align:center; background-color:#f9f9f9" ;>From Date</td> 
 
    <td style="background-color:#f9f9f9" ;><input style="width:150px;text-align:center" class="date-picker" name="oec_month1"></td> 
 
    <td style="width:150px;text-align:center; background-color:#f9f9f9" ;>To Date</td> 
 
    <td style="background-color:#f9f9f9" ;><input style="width:150px;text-align:center" class="date-picker" name="oec_month2"></td> 
 
    </tr> 
 
</table>

+0

ありがとうございます。私はJqueryUI用のライブラリを含んでいます。間違ったライブラリが含まれているため、日付選択ツールが表示されません。私の質問にお答えいただきありがとうございます – NHH

+0

@NHHそれがあなたに役立つ場合は答えとして記入してください –

関連する問題