2

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

$(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); 
    } 
    }); 
}); 

我看到别人的答案,但我没有一个工作。 一个观察,我的项目是在轨道4,我用turbolinks

+0

请详细说明_“不要工作”_。经仔细检查,你可能会在第二行之后错过一个';'。 – Halcyon

+0

试试$(document).ready – v2b

回答

1

我会建议使用您的浏览器开发人员工具(F12),以确保所有脚本都正确加载和您没有遇到任何与他们有关的错误。

你需要确保你加载必要的jQuery的脚本和jQueryUI的脚本(按顺序),而不会被调用的日期选择器()函数加载之前对这些脚本:

<!-- 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> 

Example

3

YES!谢谢!我知道了!

我偶然的功能,外观:

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

+1

谢谢!你为我节省了很多时间。 – mirap

相关问题