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

相關問題