2

我正在使用 jquery UI datepicker 并且有问题。我的日期选择器在第一次加载时不起作用。我搜索并尝试用 document.ready 替换 windows.load,但也不起作用:/ 我的代码是:

$(window).load(function() {
  alert('carregado!')   
  $('.datepicker').datepicker({ format: 'dd/mm/yyyy', language: 'pt-BR', autoclose: true});
  $('#status_bar').barrating( {
    onSelect: function(value, text) {
      $('#projeto_status').val(value);
    }
  });
});

我看到其他人的答案,但没有人和我一起工作。观察一下,我的项目在 rails 4 中,我使用turbolinks

4

2 回答 2

3

是的!谢谢!我知道了!

我碰巧这个功能,看:

var do_on_load = function(){
  alert('carregado!')   
  $('.datepicker').datepicker({ format: 'dd/mm/yyyy', language: 'pt-BR', autoclose: true});
  $('#status_bar').barrating( {
    onSelect: function(value, text) {
      $('#projeto_status').val(value);
    }
  });
}

$(document).ready(do_on_load)
$(window).bind('page:change', do_on_load)

现在,解决了!!:D

于 2013-07-31T17:21:21.243 回答
1

我建议在您的浏览器中使用开发人员工具 (F12) 以确保您的所有脚本都被正确加载并且您没有遇到任何与它们相关的错误。

您需要确保您正在加载必要的 jQuery 脚本和 jQueryUI 脚本(按此顺序),并且在加载这些脚本之前没有调用您的 datepicker() 函数:

<!-- Related jQuery and jQueryUI scripts and CSS -->
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/smoothness/jquery-ui.min.css" rel="stylesheet" type="text/css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js"></script>
<!-- Place any scripts related to your barrating plugin here -->

<script type='text/javascript'>
  //Shorthand for $(document).ready(){ ... }
  $(function(){
    alert('carregado!')   
    //Your DatePicker
    $('.datepicker').datepicker({ format: 'dd/mm/yyyy', language: 'pt-BR', autoclose: true});
    //Ensure that you are closing each of these properly
    $('#status_bar').barrating({
         onSelect: function(value, text) {
              $('#projeto_status').val(value);
         }
    });
  });
</script>

例子

于 2013-07-31T14:40:28.090 回答