2010-04-27 56 views
0

我有一個jQuery的日期選擇器腳本,我想要做的是通過選擇框更改「minDate:10」的值。 如果第一個選項被選中,minDate:10,如果第二個選項被選中,那麼minDate:20 etc ...基於選擇框更改jquery變量的值

是否每次選擇不同的選擇都會實時更新日期選擇器?

在此先感謝!

$(function() { 
       $('#hotel').change(function() { 
       // assign the value to a variable, so you can test to see if it is working 
       var selectVal = $('#hotel :selected').val(); 
       if(selectVal == "hotel_c") { 
       var date = 10; 
       } 
       if(selectVal == "hotel_a") { 
       var date = 15; 
       } 
       if(selectVal == "hotel_b") { 
       var date = 6; 
       } 
       }); 
     var dates = $('#from, #to').datepicker({ 
      defaultDate: "+1w", 
      changeMonth: true, 
      dateFormat: 'yy-m-d', 
      minDate: 10, 
      numberOfMonths: 3, 
      onSelect: function(selectedDate) { 
       var option = this.id == "from" ? "minDate" : "maxDate"; 
       var instance = $(this).data("datepicker"); 
       var date = $.datepicker.parseDate(instance.settings.dateFormat || $.datepicker._defaults.dateFormat, selectedDate, instance.settings); 
       dates.not(this).datepicker("option", option, date); 
      } 
     }); 
    }); 

回答

2

你可以寫你的更改處理是這樣的:

var hotelMap = { hotel_a: 15, hotel_b: 6, hotel_c: 10 }; 
$('#hotel').change(function() { 
    var selectVal = $('#hotel :selected').val(); 
    $("#from, #to").datepicker("option", "minDate", hotelMap[selectVal]); 
}); 

這2度的變化,我們調用了.datepicker('option', 'minDate', val)方法,我們正在使用對象映射日期範圍清理它有點。這隻會讓您根據您的選擇選擇15天,6天或10天的日期。

You can see this with the rest of your code working here

+0

非常感謝!工作正常! :) – Nikos 2010-04-27 01:17:14

0

修改您的更改處理程序,以在選擇框更改時更新日期選擇器。初始化後請參閱modifying minDate和其他選項的datepicker文檔。該功能將接受date object或表示日期的字符串。指定一個號碼將從今天起禁止date天。

$('#hotel').change(function() { 
    ... 
    var date = // somehow get the date 

    // update the date picker 
    // need to construct a date string or a date object here 
    // will disallow "date" days from today 
    $('#from, #to').datepicker('option', 'minDate', date); 
});