5

我正在尝试将不同的 jquery-ui datepicker 选项组合到一个函数中。我可以让大部分脚本在语言之外工作。我拥有所有 i18n 文件,并且正在使用选择元素来更改值。但似乎没有任何效果。我做错了什么?

  <script>
  $(function() {
    $.datepicker.setDefaults( $.datepicker.regional[ "" ] );
    $( "#from" ).datepicker({
        defaultDate: "+4w",
        changeMonth: true,
        numberOfMonths: 3,
        showButtonPanel: true,
        showOn: "button",
        buttonImage: "../images/calendar.gif",
        buttonImageOnly: true,
        minDate:"+1d",
        dateFormat:"yy/mm/dd",
        showAnim:"bounce",
        onSelect: function( selectedDate ) {
    $( "#from" ).datepicker( $.datepicker.regional[ "en-GB" ] );
    $( "#locale" ).change(function() {
        $( "#from" ).datepicker( "option",
            $.datepicker.regional[ $( this ).val() ] );
          });
        }
                        
    });
    $( "#to" ).datepicker({
        defaultDate: "+2w",
        changeMonth: true,
        numberOfMonths: 3,
        showButtonPanel: true,
        showOn: "button",
        buttonImage: "../images/calendar.gif",
        buttonImageOnly: true,
        minDate:"+1d",
        dateFormat:"yy/mm/dd",
        showAnim:"bounce",
        onSelect: function( selectedDate ) {
    $( "#to" ).datepicker( $.datepicker.regional[ "en-GB" ] );
    $( "#locale" ).change(function() {
        $( "#to" ).datepicker( "option",
            $.datepicker.regional[ $( this ).val() ] );
            });
        }
    });
 });
 </script>
4

3 回答 3

3

我已经尝试了我在下面编写的代码(从您的代码中采用)并且它有效。也许您忘记包含每种语言的 jquery javascript?我只包括了三种语言(例如法语)作为示例。此外,我还有触发语言的选择选项的更改事件,而不是触发 onSelect。

我还尝试了将其放入一个函数的版本,并且该版本有效,只是因为它触发了选择,因此必须先选择一个日期,然后才能开始工作,然后再选择一种语言。

第一个版本(在一个功能中有效,但在第一次选择日期之前不会以语言显示的问题):

<html><head><title>Test1</title>

<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<link type="text/css" href="css/ui-lightness/jquery-ui-1.8.18.custom.css" rel="Stylesheet" />   
<script type="text/javascript" src="js/jquery-ui-1.8.18.custom.min.js"></script>
<script type="text/javascript" src="jquery.ui.datepicker-fr.js"></script>
<script type="text/javascript" src="jquery.ui.datepicker-ar.js"></script>
<script type="text/javascript" src="jquery.ui.datepicker-af.js"></script>
<script type="text/javascript" src="jquery.ui.datepicker-en-GB.js"></script>
</head><body>

<script type="text/javascript">
$(function() {
    $.datepicker.setDefaults( $.datepicker.regional[ "" ] );
    $( "#from" ).datepicker({
        defaultDate: "+4w",
        changeMonth: true,
        numberOfMonths: 3,
        showButtonPanel: true,
        showOn: "button",
        minDate:"+1d",
        dateFormat:"yy/mm/dd",
        showAnim:"bounce",
    onSelect: function( selectedDate ) {
        $( "#from" ).datepicker( $.datepicker.regional[ "en-GB" ] );
        $( "#locale" ).change(function() {
            $( "#from" ).datepicker( "option",
                    $.datepicker.regional[ $( this ).val() ] );
        });
        }
  });
});

</script>

<p>Date: <input type="text" id="from"/>&nbsp;
<select id="locale">
    <option value="af">Afrikaans</option>
    <option value="sq">Albanian (Gjuha shqipe)</option>
    <option value="ar-DZ">Algerian Arabic</option>
    <option value="ar">Arabic (&#8235;(&#1604;&#1593;&#1585;&#1576;&#1610;</option>
    <option value="hy">Armenian (&#1344;&#1377;&#1397;&#1381;&#1408;&#1381;&#1398;)</option>
    <option value="az">Azerbaijani (Az&#601;rbaycan dili)</option>
    <option value="eu">Basque (Euskara)</option>
    <option value="bs">Bosnian (Bosanski)</option>
    <option value="bg">Bulgarian (&#1073;&#1098;&#1083;&#1075;&#1072;&#1088;&#1089;&#1082;&#1080; &#1077;&#1079;&#1080;&#1082;)</option>
    <option value="ca">Catalan (Catal&agrave;)</option>
    <option value="zh-HK">Chinese Hong Kong (&#32321;&#39636;&#20013;&#25991;)</option>
    <option value="zh-CN">Chinese Simplified (&#31616;&#20307;&#20013;&#25991;)</option>
    <option value="zh-TW">Chinese Traditional (&#32321;&#39636;&#20013;&#25991;)</option>
    <option value="hr">Croatian (Hrvatski jezik)</option>
    <option value="cs">Czech (&#269;e&#353;tina)</option>
    <option value="da">Danish (Dansk)</option>
    <option value="nl-BE">Dutch (Belgium)</option>
    <option value="nl">Dutch (Nederlands)</option>
    <option value="en-AU">English/Australia</option>
    <option value="en-NZ">English/New Zealand</option>
    <option value="en-GB">English/UK</option>
    <option value="eo">Esperanto</option>
    <option value="et">Estonian (eesti keel)</option>
    <option value="fo">Faroese (f&oslash;royskt)</option>
    <option value="fa">Farsi/Persian (&#8235;(&#1601;&#1575;&#1585;&#1587;&#1740;</option>
    <option value="fi">Finnish (suomi)</option>
    <option value="fr" selected="selected">French (Fran&ccedil;ais)</option>
    <option value="fr-CH">French/Swiss (Fran&ccedil;ais de Suisse)</option>
    <option value="gl">Galician</option>
    <option value="ge">Georgian</option>
    <option value="de">German (Deutsch)</option>
    <option value="el">Greek (&#917;&#955;&#955;&#951;&#957;&#953;&#954;&#940;)</option>
    <option value="he">Hebrew (&#8235;(&#1506;&#1489;&#1512;&#1497;&#1514;</option>
    <option value="hi">Hindi (&#2361;&#2367;&#2306;&#2342;&#2368;)</option>
    <option value="hu">Hungarian (Magyar)</option>
    <option value="is">Icelandic (&Otilde;slenska)</option>
    <option value="id">Indonesian (Bahasa Indonesia)</option>
    <option value="it">Italian (Italiano)</option>
    <option value="ja">Japanese (&#26085;&#26412;&#35486;)</option>
    <option value="kk">Kazakhstan (Kazakh)</option>
    <option value="km">Khmer</option>
    <option value="ko">Korean (&#54620;&#44397;&#50612;)</option>
    <option value="lv">Latvian (Latvie&ouml;u Valoda)</option>
    <option value="lt">Lithuanian (lietuviu kalba)</option>
    <option value="lb">Luxembourgish</option>
    <option value="mk">Macedonian</option>
    <option value="ml">Malayalam</option>
    <option value="ms">Malaysian (Bahasa Malaysia)</option>
    <option value="no">Norwegian (Norsk)</option>
    <option value="pl">Polish (Polski)</option>
    <option value="pt">Portuguese (Portugu&ecirc;s)</option>
    <option value="pt-BR">Portuguese/Brazilian (Portugu&ecirc;s)</option>
    <option value="rm">Rhaeto-Romanic (Romansh)</option>
    <option value="ro">Romanian (Rom&acirc;n&#259;)</option>
    <option value="ru">Russian (&#1056;&#1091;&#1089;&#1089;&#1082;&#1080;&#1081;)</option>
    <option value="sr">Serbian (&#1089;&#1088;&#1087;&#1089;&#1082;&#1080; &#1112;&#1077;&#1079;&#1080;&#1082;)</option>
    <option value="sr-SR">Serbian (srpski jezik)</option>
    <option value="sk">Slovak (Slovencina)</option>
    <option value="sl">Slovenian (Slovenski Jezik)</option>
    <option value="es">Spanish (Espa&ntilde;ol)</option>
    <option value="sv">Swedish (Svenska)</option>
    <option value="ta">Tamil (&#2980;&#2990;&#3007;&#2996;&#3021;)</option>
    <option value="th">Thai (&#3616;&#3634;&#3625;&#3634;&#3652;&#3607;&#3618;)</option>
    <option value="tj">Tajikistan</option>
    <option value="tr">Turkish (T&uuml;rk&ccedil;e)</option>
    <option value="uk">Ukranian (&#1059;&#1082;&#1088;&#1072;&#1111;&#1085;&#1089;&#1100;&#1082;&#1072;)</option>
    <option value="vi">Vietnamese (Ti&#7871;ng Vi&#7879;t)</option>
    <option value="cy-GB">Welsh/UK (Cymraeg)</option>
</select></p>


</body></html>

第二个版本(不是在一个功能中,也可以工作,对我来说似乎更好,因为您不必选择日期来更改语言):

<html><head><title>Test1</title>

<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<link type="text/css" href="css/ui-lightness/jquery-ui-1.8.18.custom.css" rel="Stylesheet" />   
<script type="text/javascript" src="js/jquery-ui-1.8.18.custom.min.js"></script>
<script type="text/javascript" src="jquery.ui.datepicker-fr.js"></script>
<script type="text/javascript" src="jquery.ui.datepicker-ar.js"></script>
<script type="text/javascript" src="jquery.ui.datepicker-af.js"></script>
</head><body>

<script type="text/javascript">
$(function() {
    $.datepicker.setDefaults( $.datepicker.regional[ "" ] );
    $( "#from" ).datepicker({
        defaultDate: "+4w",
        changeMonth: true,
        numberOfMonths: 3,
        showButtonPanel: true,
        showOn: "button",
        minDate:"+1d",
        dateFormat:"yy/mm/dd",
        showAnim:"bounce"
    });
$( "#from" ).datepicker( $.datepicker.regional[ "en-GB" ] );
    $( "#locale" ).change(function() {
        $( "#from" ).datepicker( "option",
             $.datepicker.regional[ $( this ).val() ] );
    });
});

</script>

<p>Date: <input type="text" id="from"/>&nbsp;
<select id="locale">
    <option value="af">Afrikaans</option>
    <option value="sq">Albanian (Gjuha shqipe)</option>
    <option value="ar-DZ">Algerian Arabic</option>
    <option value="ar">Arabic (&#8235;(&#1604;&#1593;&#1585;&#1576;&#1610;</option>
    <option value="hy">Armenian (&#1344;&#1377;&#1397;&#1381;&#1408;&#1381;&#1398;)</option>
    <option value="az">Azerbaijani (Az&#601;rbaycan dili)</option>
    <option value="eu">Basque (Euskara)</option>
    <option value="bs">Bosnian (Bosanski)</option>
    <option value="bg">Bulgarian (&#1073;&#1098;&#1083;&#1075;&#1072;&#1088;&#1089;&#1082;&#1080; &#1077;&#1079;&#1080;&#1082;)</option>
    <option value="ca">Catalan (Catal&agrave;)</option>
    <option value="zh-HK">Chinese Hong Kong (&#32321;&#39636;&#20013;&#25991;)</option>
    <option value="zh-CN">Chinese Simplified (&#31616;&#20307;&#20013;&#25991;)</option>
    <option value="zh-TW">Chinese Traditional (&#32321;&#39636;&#20013;&#25991;)</option>
    <option value="hr">Croatian (Hrvatski jezik)</option>
    <option value="cs">Czech (&#269;e&#353;tina)</option>
    <option value="da">Danish (Dansk)</option>
    <option value="nl-BE">Dutch (Belgium)</option>
    <option value="nl">Dutch (Nederlands)</option>
    <option value="en-AU">English/Australia</option>
    <option value="en-NZ">English/New Zealand</option>
    <option value="en-GB">English/UK</option>
    <option value="eo">Esperanto</option>
    <option value="et">Estonian (eesti keel)</option>
    <option value="fo">Faroese (f&oslash;royskt)</option>
    <option value="fa">Farsi/Persian (&#8235;(&#1601;&#1575;&#1585;&#1587;&#1740;</option>
    <option value="fi">Finnish (suomi)</option>
    <option value="fr" selected="selected">French (Fran&ccedil;ais)</option>
    <option value="fr-CH">French/Swiss (Fran&ccedil;ais de Suisse)</option>
    <option value="gl">Galician</option>
    <option value="ge">Georgian</option>
    <option value="de">German (Deutsch)</option>
    <option value="el">Greek (&#917;&#955;&#955;&#951;&#957;&#953;&#954;&#940;)</option>
    <option value="he">Hebrew (&#8235;(&#1506;&#1489;&#1512;&#1497;&#1514;</option>
    <option value="hi">Hindi (&#2361;&#2367;&#2306;&#2342;&#2368;)</option>
    <option value="hu">Hungarian (Magyar)</option>
    <option value="is">Icelandic (&Otilde;slenska)</option>
    <option value="id">Indonesian (Bahasa Indonesia)</option>
    <option value="it">Italian (Italiano)</option>
    <option value="ja">Japanese (&#26085;&#26412;&#35486;)</option>
    <option value="kk">Kazakhstan (Kazakh)</option>
    <option value="km">Khmer</option>
    <option value="ko">Korean (&#54620;&#44397;&#50612;)</option>
    <option value="lv">Latvian (Latvie&ouml;u Valoda)</option>
    <option value="lt">Lithuanian (lietuviu kalba)</option>
    <option value="lb">Luxembourgish</option>
    <option value="mk">Macedonian</option>
    <option value="ml">Malayalam</option>
    <option value="ms">Malaysian (Bahasa Malaysia)</option>
    <option value="no">Norwegian (Norsk)</option>
    <option value="pl">Polish (Polski)</option>
    <option value="pt">Portuguese (Portugu&ecirc;s)</option>
    <option value="pt-BR">Portuguese/Brazilian (Portugu&ecirc;s)</option>
    <option value="rm">Rhaeto-Romanic (Romansh)</option>
    <option value="ro">Romanian (Rom&acirc;n&#259;)</option>
    <option value="ru">Russian (&#1056;&#1091;&#1089;&#1089;&#1082;&#1080;&#1081;)</option>
    <option value="sr">Serbian (&#1089;&#1088;&#1087;&#1089;&#1082;&#1080; &#1112;&#1077;&#1079;&#1080;&#1082;)</option>
    <option value="sr-SR">Serbian (srpski jezik)</option>
    <option value="sk">Slovak (Slovencina)</option>
    <option value="sl">Slovenian (Slovenski Jezik)</option>
    <option value="es">Spanish (Espa&ntilde;ol)</option>
    <option value="sv">Swedish (Svenska)</option>
    <option value="ta">Tamil (&#2980;&#2990;&#3007;&#2996;&#3021;)</option>
    <option value="th">Thai (&#3616;&#3634;&#3625;&#3634;&#3652;&#3607;&#3618;)</option>
    <option value="tj">Tajikistan</option>
    <option value="tr">Turkish (T&uuml;rk&ccedil;e)</option>
    <option value="uk">Ukranian (&#1059;&#1082;&#1088;&#1072;&#1111;&#1085;&#1089;&#1100;&#1082;&#1072;)</option>
    <option value="vi">Vietnamese (Ti&#7871;ng Vi&#7879;t)</option>
    <option value="cy-GB">Welsh/UK (Cymraeg)</option>
</select></p>


</body></html>

参考:

http://jqueryui.com/demos/datepicker/#localization

http://jquery-ui.googlecode.com/svn/trunk/ui/i18n/

于 2012-07-20T02:11:46.467 回答
3

好吧,我花了将近一整天的时间让本地化在我的 jQuery DatePicker 上工作:

第 1 步:在一个通用文件中创建一个 JS 文件,其中包含所有语言(您希望其翻译出现)的月份名称等的所有翻译,并将其命名为jquery.ui.datepicker-common.js(或您所用的任何名称)喜欢)(您也可以为所有必需的语言创建单独的 JS 文件,并将其命名为 -- jquery.ui.datepicker-fr.js for French File)

第 2 步:将此 JS 文件包含在您希望显示翻译值的网页中。

<script src="Assets/JS/JQuery/jquery.ui.datepicker-fr.js" type="text/javascript"></script>

第 3 步:按照此示例使 DatePicker 使用翻译后的语言。将您的日期选择器名称粘贴到DatePickerName. 粘贴包含当前语言的控件名称hdn_Locale(如示例中所用)。

$("#DatePickerName").datepicker({
         changeMonth: true,
         changeYear: true,
         defaultDate: "+4w",
         minDate: "+1d",
         dateFormat: "yy/mm/dd",
         onSelect: function (selectedDate) {
                               $("#DatePickerName").datepicker(navigator.userLanguage,
                                   $.datepicker.regional[$('#hdn_Locale').val()]);
         }
});
于 2013-01-23T11:45:44.487 回答
0

我设法让这个工作我已经将语言包装在选择部分的 jquery ui 中,因为我已经声明了 dateformat 和 mindate 选项,所以在一切正常工作后,我之前已经声明了代码

<script type="text/javascript">
//<![CDATA[
$(function() {
    $.datepicker.setDefaults( $.datepicker.regional[ "" ] );
    $( "#from" ).datepicker({
        defaultDate: "+4w",
        changeMonth: true,
        numberOfMonths: 2,
        showButtonPanel: true,
        showOn: "button",
        buttonImage: "../images/calendar.gif",
        buttonImageOnly: true,
        minDate:"+1d",
        dateFormat:"yy/mm/dd",
        showAnim:"blind",           
    });
    $( "#from" ).datepicker( $.datepicker.regional[ "en-GB" ] );
    $( "#locale" ).change(function() {
        $( "#from" ).datepicker( "option",
            $.datepicker.regional[ $( this ).val() ] );
          });
    $( "#to" ).datepicker({
        defaultDate: "+2w",
        changeMonth: true,
        numberOfMonths: 2,
        showButtonPanel: true,
        showOn: "button",
        buttonImage: "../images/calendar.gif",
        buttonImageOnly: true,
        minDate:"",
        dateFormat:"yy/mm/dd",
        showAnim:"blind",
        });
    $( "#to" ).datepicker( $.datepicker.regional[ "en-GB" ] );
    $( "#locale" ).change(function() {
        $( "#to" ).datepicker( "option",
            $.datepicker.regional[ $( this ).val() ] );
            });
 });
//]]>
</script>
于 2012-07-21T00:52:05.290 回答