2017-02-20 172 views
0

我有一個預訂表單,我只需要兩個輸入#checkin和#checkout,這就是爲什麼我用datepicker與daterange一切都會好的幾乎有一件事是我怎麼才能選擇15天之間兩個約會?設置日期範圍與pikaday datepicker

順便說一下我使用Pikaday datepicker

$(document).ready(function() { 
 

 
    $('.flexdatalist').flexdatalist({ 
 
    minLength: 0, 
 
    searchContain: true, 
 
    }).on('select:flexdatalist', function() { 
 
    $('#checkin').trigger("click"); 
 
    }); 
 

 
    assignPicker = function(id, whenClosed) { 
 
    if (typeof whenClosed !== 'function') { 
 
     whenClosed = null; 
 
    } 
 

 
    return new Pikaday({ 
 
     numberOfMonths: 2, 
 
     field: document.getElementById(id), 
 
     format: "DD.MM.YYYY", 
 
     minDate: new Date(), 
 
     firstDay: 1, 
 
     maxDate: new Date(2020, 12, 31), 
 
     onSelect: function() { 
 
     e = this.getDate(); 
 
     }, 
 
     onSelect: whenClosed 
 
    }); 
 
    } 
 

 
    assignPicker('checkin', function() { 
 
    $('#checkout').trigger("click"); 
 
    }); 
 
    assignPicker('checkout', function() { 
 
    $('#select').trigger("click"); 
 
    }); 
 

 
});
body { 
 
      padding: 30px; 
 
     } 
 
     input, 
 
     select { 
 
      width: 100%; 
 
      padding: 10px; 
 
      border: 1px solid #ccc; 
 
     }
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> 
 
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pikaday/1.5.1/css/pikaday.min.css" /> 
 
    <link rel="stylesheet" href="https://cdn.anitur.com.tr/example/flexdatalist/flexdatalist.css" /> 
 

 

 

 

 
    <div class="container"> 
 

 

 
    <div class="row"> 
 
     <div class="col-lg-3 col-md-3 col-sm-3"> 
 
     <input type="text" name="" class='flexdatalist' data-min-length='1' list='languages' name='language' /> 
 
     <datalist id="languages"> 
 
      <option value="PHP">PHP</option> 
 
      <option value="JavaScript">JavaScript</option> 
 
      <option value="Cobol">Cobol</option> 
 
      <option value="C#">C#</option> 
 
      <option value="C++">C++</option> 
 
      <option value="Java">Java</option> 
 
      <option value="Pascal">Pascal</option> 
 
      <option value="FORTRAN">FORTRAN</option> 
 
      <option value="Lisp">Lisp</option> 
 
      <option value="Swift">Swift</option> 
 
      <option value="ActionScript">ActionScript</option> 
 
     </datalist> 
 
     </div> 
 
     <div class="col-lg-3 col-md-3 col-sm-3"> 
 
      <input type="text" id="checkin" /> 
 
     </div> 
 
     <div class="col-lg-3 col-md-3 col-sm-3"> 
 

 
      <input type="text" id="checkout" /> 
 
     </div> 
 
     <div class="col-lg-3 col-md-3 col-sm-3"> 
 

 
<form action="" method="get"> 
 
     <select name="select" id="select"> 
 
      <option value="0">Choose</option> 
 
      <option value="1">1</option> 
 
      <option value="2">2</option> 
 
      <option value="3">3</option> 
 
      <option value="4">4</option> 
 
      <option value="5">5</option> 
 
      <option value="6">6</option> 
 
      <option value="7">7</option> 
 
     </select> 
 
     </form> 
 
     </div> 
 
    </div> 
 

 

 
    </div> 
 

 

 

 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
 
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> 
 
    
 
    <script src="https://cdnjs.cloudflare.com/ajax/libs/pikaday/1.5.1/pikaday.min.js"></script> 
 

 
    <script src="https://cdn.anitur.com.tr/example/flexdatalist/flexdatalist.js"></script>

if you can check it out on codepen instead of stackoverflow snippet

回答

1

你可以保存到收銀臺選擇器的參考,然後綁定到籤揀貨機的onSelect方法的功能並相應地調整結帳日期選擇器。您可以(也可能應該)加載Moment.js以使日期計算更容易。 Pikaday(你正在使用)的format屬性在沒有它的情況下甚至不會工作。

$(document).ready(function() { 

    $('.flexdatalist').flexdatalist({ 
    minLength: 0, 
    searchContain: true, 
    }).on('select:flexdatalist', function() { 
    $('#checkin').trigger("click"); 
    }); 

    assignPicker = function(id, whenClosed) { 
    if (typeof whenClosed !== 'function') { 
     whenClosed = null; 
    } 

    return new Pikaday({ 
     numberOfMonths: 2, 
     field: document.getElementById(id), 
     format: "DD.MM.YYYY", 
     minDate: new Date(), 
     firstDay: 1, 
     maxDate: new Date(2020, 12, 31), 
     onSelect: whenClosed 
    }); 
    } 

    var checkoutPicker = assignPicker('checkout', function() { 
    $('#select').trigger("click"); 
    }); 

    var checkinPicker = assignPicker('checkin', function() { 
    var maxDate = this.getMoment().add(15, 'days'); 
    checkoutPicker.setMaxDate(maxDate.toDate()); 
    checkoutPicker.setMinDate(this.getDate()); // min date of checkout = checkin 
    checkoutPicker.setDate(null); 
    $('#checkout').trigger("click"); // trigger checkout picker 
    }); 

}); 

http://codepen.io/anon/pen/apgGpN

+0

thanks.After籤它不注重形式結賬 –

+0

我想這是關於getMoment的問題,我不想getmoment.js有什麼辦法沒有moment.js辦? –

+1

只需將'$('#checkout')。trigger(「click」);'添加到checkin picker回調函數中。當然,你不必使用Moment.js,看看[這裏](http://stackoverflow.com/questions/563406/add-days-to-javascript-date)。但是在JS中使用日期時(日光節約,閏年,時區等)需要考慮很多事情,而Moment會處理所有事情。它讓生活變得更容易,所以我建議你使用它。 –