0

我需要使用 JavaScript 中 onclick 事件处理程序的单选按钮中的参数向日期对象添加一定数量的天数。当我只是将日期添加到末尾时,它只会在日期末尾添加 1 等。有任何想法吗?

var dateObject = new Date();
var month = dateObject.getMonth();

var monthArray = new Array("January", "February", "March", "April",  
                           "May", "June", "July", "August",
                           "September","October","November","December");

function orderReadyDate(orderTime) 
{
    var dateToday = monthArray[month] + " " + dateObject.getDate() + ", " + dateObject.getFullYear();
    document.forms[0].txtPickupDate.value = dateToday;
} 

这是我的输入按钮

<input type="radio" name="item" value="print_5x7" onclick="orderReadyDate(1)" />5x7 Prints(1 day)<br />
<input type="radio" name="item" value="poster" onclick="orderReadyDate(1)" />Poster (1 day)<br />
<input type="radio" name="item" value="mug" onclick="orderReadyDate(2)" />Coffee Mug (2 days)<br />
<input type="radio" name="item" value="shirt" onclick="orderReadyDate(3)" />T-shirt (3 days)</p>
4

2 回答 2

0
var dateToday = monthArray[month] + " " 
        + (dateObject.getDate() + orderTime) 
        + ", " + dateObject.getFullYear();
于 2013-03-20T01:37:28.000 回答
0

您需要尝试以下操作:- 请注意 setDate 方法,该方法将添加日期。

 var newdate = new Date();
 newdate.setDate(newdate.getDate() + orderTime);
 var dateToday = monthArray[newdate.getMonth()] + " " + 
                 newdate.getDate() + ", " + 
                 newdate.getFullYear();
于 2013-03-20T01:50:44.317 回答