2013-10-05 53 views
-2

是否可以更改所選數據的值?更改值datepicker

因此,當他們選擇10月份時,它會提交value =「10」,但它必須更改爲value =「9」。

這可能嗎?

我使用下面的代碼:

<script type='text/javascript'> 
$(window).load(function(){ 
$(".date_year").datepicker({ 
    showOn: 'button', 
    //buttonImage: "calendar_icon.png", 
    buttonText: 'Cal', 
    buttonImageOnly: false, 
    showAnim: 'fadeIn', 
    dateFormat: 'dd/mm/yy', 



    onClose: function (dateText, picker) { 
     dateArr = dateText.split('/'); 
     $(this).siblings('.date_month').val(dateArr[1]); 
     $(this).siblings('.date_day').val(dateArr[0]); 
     $(this).val(dateArr[2]); 
    } 
}); 
});> 
</script> 

而我的HTML是:

<div class="date-picker"> 
    <label for="preferredDate">Arrival</label> 
    <input type="text" class="date_day" name="day_a" />/ 
    <input type="text" class="date_month" name="month_a" />/ 
    <input type="text" class="date_year" name="year_a" /> 
</div> 
<div class="date-picker"> 
    <label for="preferredDate">Departure</label> 
    <input type="text" class="date_day" name="day_d" />/ 
    <input type="text" class="date_month" name="month_d" />/ 
    <input type="text" class="date_year" name="year_d"/> 
</div> 

回答

1

您已經在使用時,用戶完成採摘修改日期的功能,我米不知道你是否自己寫了或從某處得到它,但讓我解釋一下:

// when the user is done picking the date, this function runs 
onClose: function (dateText, picker) { 
    //the date is split into day, month year (by taking the pieces between /'s) 
    dateArr = dateText.split('/'); 

    // the second part of the split date string is the month, put it in the .date_month box 
    $(this).siblings('.date_month').val(dateArr[1]); 

    // same with the day and year respectively 
    $(this).siblings('.date_day').val(dateArr[0]); 
    $(this).val(dateArr[2]); 
} 

在喲你把日期放在方框中,你可以添加一些額外的腳本。更換與設置.date_month值行:

var month = dateArr[1]; 
var newMonth = month - 1; //or whatever changes you want to make to the month 
$(this).siblings('.date_month').val(newMonth); 

當然你可以做到這一小段路:

$(this).siblings('.date_month').val(newMonth - 1); 

我分裂它展現出位更清楚發生了什麼,並應也可以看到在newMonth部分,如果您想在實際將其放入月份字段之前,您可以進行更多計算。

+0

感謝斯蒂芬爲您的答覆,這樣做的工作,但問題是,更改後的值現在可以在輸入字段中看到。用戶可能看不到更改的值,只有將正確的值發送到其他服務器纔是重要的。這可能嗎? – steven

+0

然後這不是日期選擇器可以做的事情。所有的日期選擇器都會填寫一個字段,將字段提交給服務器不是腳本的一部分。您必須製作一個腳本,在發送之前更改提交字段的值。 –