Basically I have HTML form which reads in a date from a user, for a hotel booki开发者_JAVA百科ng system.
I have 3 lists, one for day, one for month and one for year.
<SELECT NAME="month">
<OPTION VALUE="1"> Jan
<OPTION VALUE="2"> Feb
<OPTION VALUE="3"> March and so on
</SELECT>
and the same for year(2010 - 2013) and day(1 -31).
Is there any way I can have the default value in the day list set to the current day, the default value in the month list set the the current month, and the default value in the year list set to the current year? Rather than manually having to do it.
Thanks in advance for the help!
You will need to use JavaScript. Here's an example, using jQuery:
<script type="text/javascript">
$(document).ready(function(){
var CurrentDate=new Date();
$("#year").val(CurrentDate.getYear());
$("#month").val(CurrentDate.getMonth());
$("#day").val(CurrentDate.getDate());
});
</script>
Here's the code in action.
You will have to use javascript. or any client side scripting to do this. I can suggest you some examples but you must first understand the concept of client side scripting
In response to @sushil bharwani you can also use a server side language if you are comfortable with that. PHP is installed on most web hosts you find now days and a plus (as opposed to javascript) is that it is guaranteed to work. The clients user agent does not matter.
This is php code:
<?php
$month = date('F');
if($month == "January") { $jan = "selected"; }
if($month == "February") { $feb = "selected"; }
if($month == "March") { $mar = "selected"; }
if($month == "April") { $apr = "selected"; }
if($month == "May") { $may = "selected"; }
if($month == "June") { $june = "selected"; }
if($month == "July") { $july = "selected"; }
if($month == "August") { $aug = "selected"; }
if($month == "September") { $sep = "selected"; }
if($month == "Octomber") { $oct = "selected"; }
if($month == "November") { $nov = "selected"; }
if($month == "December") { $dec = "selected"; }
?>
Now select below code, then copy and paste between the body-tag:
<select name="start_month" id="start_month">
<option value="January" <?php echo $jan; ?> >January</option>
<option value="February" <?php echo $feb; ?> >February</option>
<option value="March" <?php echo $march; ?> >March</option>
<option value="April" <?php echo $apr; ?> >April</option>
<option value="May" <?php echo $may; ?> >May</option>
<option value="June" <?php echo $june; ?> >June</option>
<option value="July" <?php echo $july; ?> >July</option>
<option value="August" <?php echo $aug; ?> >August</option>
<option value="September" <?php echo $sep; ?> >September</option>
<option value="Octomber" <?php echo $oct; ?> >Octomber</option>
<option value="November" <?php echo $nov; ?> >November</option>
<option value="December" <?php echo $dec; ?> >December</option>
</select>
Thanks.
Note: Link for the complete example: http://www.javascriptkit.com/script/script2/curdateform2.shtml
精彩评论