JQuery UI datepicker not working when bound to a class

拜拜、爱过 提交于 2019-12-12 08:21:47

问题


I've got this code:

<html>
<head>
  <link type="text/css" href="css/blitzer/jquery-ui-1.7.2.custom.css" rel="stylesheet" />   
  <script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
  <script type="text/javascript" src="js/jquery-ui-1.7.2.custom.min.js"></script>
  <script type="text/javascript">
$(function() {
  $('.hasDatepicker').datepicker();
});
</script>
</head>
<body>
<p>date: <input type="text" name="data" class="hasDatepicker" /></p>
<input type="submit" value="send" />
</div>
</body>
</html>

When I click the input field, nothing happens. The datepicker is initialized though, when I inspect the DOM in Firebug, I get the id="dp1260260566059" added to my <input> element.

After changing the html and js to use id attribute instead of class, so having this in my code:

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

and

<p>date: <input type="text" name="data" id="hasDatepicker" /></p>

Everything works OK.

Can't datepicker from JQuery UI work for all elements of some class?


回答1:


Please check if the datepicker works if you change the class name of the input fields from 'hasDatePicker' to something else. The reason is, when add the datepicker to the input field using the below code:

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

it adds the class name "hasDatePicker" to the input field. (which is the one u r using)




回答2:


Don't use hasDatepicker as your date picker's class name. Use a different name, like date_picker.

JQuery adds the class hasDatepicker to the element that datepicker() is called on. If the element already has the class hasDatepicker, then the datepicker() function does nothing. So, just use a different class name.



来源:https://stackoverflow.com/questions/1865492/jquery-ui-datepicker-not-working-when-bound-to-a-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!