2013-10-05 45 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

然后这不是日期选择器可以做的事情。所有的日期选择器都会填写一个字段,将字段提交给服务器不是脚本的一部分。您必须制作一个脚本,在发送之前更改提交字段的值。 –