2016-09-29 3 views
1

私はいくつかの日付のためにユーザーに尋ねているWebフォームを持っています。通常、ですが、必ずしもであるとは限りません。繰り返し日付は人為的なエラーになります。したがって、onSelect関数では、日付がすでに入力されているかどうかを確認し、重複した日付が意図的であったかどうかを確認するようユーザーに依頼します。ユーザーがいいえと言う場合、ピッカーから日付の値をクリアする方法は?あなたが与えられたオプションでそれを行うことができますが、_selectDateをオーバーライドして、条件を追加することができjQueryでonSelectで選択された日付を拒否する方法DatePicker

// datesList initialized in outer scope 

onSelect: function (thedate, picker) { 


    if ($.inArray(new Date(thedate).valueOf(), datesList) == -1) { 
     //store chosen dates in datesList if we haven't seen it before 
     datesList.push(new Date(thedate).valueOf()) 

     } else { 
      // ask the user if it was intentional 
      //if unintentional, reject the choice and clear the picker 


     } 

    } 

答えて

1

わかりません。このような何か:

$.datepicker._selectDate = function(id, dateStr) { 
 
    var target = $(id); 
 
    var inst = this._getInst(target[0]); 
 
    dateStr = (dateStr != null ? dateStr : this._formatDate(inst)); 
 
    if (inst.input) 
 
    inst.input.val(dateStr); 
 
    this._updateAlternate(inst); 
 
    var onSelect = this._get(inst, 'onSelect'); 
 
    if (onSelect){ 
 
    // you get the value of onSelect 
 
    var shouldHide = onSelect.apply((inst.input ? inst.input[0] : null), [dateStr, inst]); // trigger custom callback 
 
    } else if (inst.input && shouldHide){ 
 
    inst.input.trigger('change'); // fire the change event 
 
     } 
 
    if (inst.inline) 
 
    this._updateDatepicker(inst); 
 
    // If onSelect return false, you don't hide the datepicker 
 
    else if (shouldHide) { 
 

 
    this._hideDatepicker(); 
 
    this._lastInput = inst.input[0]; 
 
    if (typeof(inst.input[0]) != 'object') 
 
     inst.input.focus(); // restore focus 
 
    this._lastInput = null; 
 
    } 
 
} 
 

 

 
$('input').datepicker({ 
 
    onSelect: function(e, ui) { 
 
    if (confirm('OK?')) { 
 

 
     return true; 
 
    } else { 
 
     this.value = ""; 
 
     return false; 
 
    } 
 
    } 
 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js" integrity="sha256-T0Vest3yCU7pafRw9r+settMBX6JkKN06dqBnpQ8d30=" crossorigin="anonymous"></script> 
 
<link rel="stylesheet" type="text/css" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"></link> 
 
<input type=text></input>

+0

感謝。重複が意図せずにあった場合、ピッカーを隠さないという特徴が好きです。 – Tim

関連する問題