如何根据用户的选择使用jQuery进行时间戳记 [英] How to timestamp with jQuery based off user's selection

查看:50
本文介绍了如何根据用户的选择使用jQuery进行时间戳记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据用户的选择生成时间戳.

I'm attempting to produced a timestamp based off user's selection.

我有一个输入值为空的表格:

I have a form with an input value that will be empty:

<input type="hidden" name="sub_enddate" value=" " />

我需要根据用户的选择,在未来某个时间值的基础上用时间戳记填充空值:

I need to have the empty value filled in with a time stamp based a certain value of time in the future off a user's selection:

<select name="Membership Duration">
<option value="">Select A Membership Duration</option>
<option class="6" value="6 Months">6 Month Membership</option>
<option class="3" value="3 Months">3 Month Membership</option>
<option class="1" value="1 Months">1 Month Membership</option>
</select>

格式如下:20110325(2011-03-25).因此,如果用户从下拉菜单中选择:

The format will be the following: 20110325 (2011-03-25). So if the user chooses from the drop down menu:

  • "6个月会员资格"在隐藏的输入字段中的值为:20110925(从今天(2011年3月25日起,未来6个月).
  • 隐藏的输入字段中的"3个月成员资格"值将为:20110625(从今天(2011年3月25日起,未来3个月).
  • 隐藏的输入字段中的"1个月成员资格"值将为:20110425(从今天(2011年3月25日起,未来1个月).

推荐答案

请参见以下示例→ ;

这应该可以解决问题:

$('#mem_dur').change(function(e) {
    var tVal = $(this).find('option:selected').val()[0],
        tDate = new Date(),
        mDate, mDateMS, mDateStr
        oneMonth = 31 * 86400000;

    mDateMS = tDate.getTime() + tVal * oneMonth;
    mDate = new Date(mDateMS);
    mDateStr = String(mDate.getFullYear()) + 
               ((mDate.getMonth() < 9) ? "0" + (mDate.getMonth() + 1) : mDate.getMonth() + 1) + 
               ((mDate.getDate() < 10) ? "0" + mDate.getDate() : mDate.getDate());

    mDateStr = (isNaN(parseInt(mDateStr,10))) ? "" : mDateStr;

    $('#sub_enddate').val(mDateStr);
});

这篇关于如何根据用户的选择使用jQuery进行时间戳记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆