简体   繁体   English

根据单选按钮选择,为日期添加价值

[英]Add value to date based on radio button selection

I seem to be locating answers here and other locations that are close to what I need -- so my apologies if I've duplicated without noticing. 我似乎在这里找到答案,而其他位置都在我需要的位置附近,因此,如果我在没有注意的情况下重复,我会深表歉意。 And I'm certain that what I've been locating can work...if I knew how to use it :) 而且我敢肯定,我一直在寻找的东西都可以工作...如果我知道如何使用它:)

I have 4 radio buttons on a page. 我在页面上有4个单选按钮。 They have values as 4, 7, 14, 30. 它们的值为4、7、14、30。

<input type="radio" id="radio1" name="length" value="4"/><label for="radio1">4 DAYS</label>
<input type="radio" id="radio2" name="length" value="7"/><label for="radio2">7 DAYS</label>
<input type="radio" id="radio3" name="length" value="14"/><label for="radio3">14 DAYS</label>
<input type="radio" id="radio4" name="length" value="30"/><label for="radio4">30 DAYS</label>

A return date is needed based on the above selection. 根据以上选择,需要返回日期。 Say if radio2 is selected, the return date will be the date selected + 7 days. 假设如果选择radio2,则返回日期将为所选日期+ 7天。

Here http://jsfiddle.net/JKGvD/525/ is a great example of this functionality however you can see I'm attempting to manipulate the 'nights' field with user input from above. 这里http://jsfiddle.net/JKGvD/525/是此功能的一个很好的例子,但是您可以看到我正在尝试通过上面的用户输入来操纵'nights'字段。

I attempted to use the $('input:radio[name=length]:checked').val(); 我试图使用$('input:radio[name=length]:checked').val(); in that Fiddle however it fails with Nan (Not a Number?) in the field. 在那个小提琴中,它在场上以Nan(Not a Number?)失败。

Please alert me if any further information is needed. 如果需要任何进一步的信息,请提醒我。 And thank you in advance everyone! 并预先感谢大家!

function addMinutes(date, minutes) {
    return new Date(date.getTime() + minutes*60000);
}

per

How to add 30 minutes to a JavaScript Date object? 如何将30分钟添加到JavaScript Date对象?

Converting your days to minutes we have 1 day = 24 hours = 1440 minutes. 将您的天数转换为分钟,我们有1天= 24小时= 1440分钟。

So if you want it in days change the constant accordingly by multiplying by 1440. 因此,如果您希望在几天之内将其乘以1440,就可以相应地更改常数。

To get the radio button value use 要获取单选按钮的值,请使用

$('input[name=radioName]:checked', '#myForm').val()

per

How can I know which radio button is selected via jQuery? 我怎么知道通过jQuery选择了哪个单选按钮?

Try casting it to a number, like: 尝试将其转换为数字,例如:

+$('input:radio[name=length]:checked').val();

Remember that HTML values are Strings. 请记住,HTML值是字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM