简体   繁体   中英

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. They have values as 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.

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.

I attempted to use the $('input:radio[name=length]:checked').val(); in that Fiddle however it fails with Nan (Not a Number?) in the field.

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?

Converting your days to minutes we have 1 day = 24 hours = 1440 minutes.

So if you want it in days change the constant accordingly by multiplying by 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?

Try casting it to a number, like:

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

Remember that HTML values are Strings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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