Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

jquery - Bootstrap Datepicker Not Working

I am trying to get bootstrap datepicker to work but it does not even show up.
http://www.eyecon.ro/bootstrap-datepicker/

<link href="~/Css/datepicker.css" rel="stylesheet" />
<script src="~/Scripts/jquery-1.8.1.js" type="text/javascript"></script>
<script src="~/Scripts/bootstrap-datepicker.js" type="text/javascript"></script>
<script type="text/javascript">
    $("#datepicker").datepicker();
</script>

<input id="datepicker" value="10/22/2011" />

I am using bootstrap 2.1.1

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

It looks like you're trying to initialize a datepicker when there's no #datepicker element in DOM yet (the corresponding input follows <script> element, not vice versa).

As $('#datepicker') will still return a jQuery-wrapped object (not null!), no error will be thrown.

To solve this, either make this call when DOM is loaded:

$(function() {
  $('#datepicker').datepicker();
});

or move this script into very end of <body> element.

I'd choose the first option; moving all the DOM-ready actions into the same $(function() { ... }); wrapper as well.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...