Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
394 views
in Technique[技术] by (71.8m points)

javascript - Setting default date for Jqueryui datepicker

I have a simple form for generating reports that I am adding a jqueryUI datepicker to. The problem I am having is when I try to repopulate the fields with the info from the user.

Jquery seems to be resetting the default value on my date fields (currently, it picks 2 different dates in July). How can I set it back to the user submitted value?

My current code:

<form method="post" action="">
        Username: <input type="text" size="12" name="username" required  value="foo" />
        Password: <input type="password" size="12" name="password" required value="bar" />
        Start Date: <input id="startdate" type="text" size="12" name="startday" required  value="06/28/2012" />
        End Date: <input id="enddate" type="text" size="12" name="endday" required  value="06/30/2012" />

        <script type="text/javascript">
            $(function(){
                $("#startdate").datepicker();
                $("#enddate").datepicker();
            });
        </script>
        <input type="submit">
    </form>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Set the date to highlight on first opening if the field is blank. Specify either an actual date via a Date object or as a string in the current dateFormat, or a number of days from today (e.g. +7) or a string of values and periods ('y' for years, 'm' for months, 'w' for weeks, 'd' for days, e.g. '+1m +7d'), or null for today.

Initialize a datepicker with the defaultDate option specified.

$( ".selector" ).datepicker({ defaultDate: +7 });

Get or set the defaultDate option, after init.

//getter
var defaultDate = $( ".selector" ).datepicker( "option", "defaultDate" );

//setter
$( ".selector" ).datepicker( "option", "defaultDate", +7 );

http://jqueryui.com/demos/datepicker/#option-defaultDate

Fiddle: http://jsfiddle.net/iambriansreed/gMNrZ/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...