简体   繁体   English

Javascript 日期选择器 asp.net mvc

[英]Javascript date picker asp.net mvc

I'm new working with asp.net and I was very happy about the Html Helper that make editor for dates but I saw that it's not compatible with safari so I decided to use javascript date picker.我是 asp.net 的新成员,我对制作日期编辑器的 Html Helper 感到非常高兴,但我发现它与 safari 不兼容,所以我决定使用 javascript 日期选择器。

So I installed jquery.ui.combined, placed every(maybe too many)render or <script> and I tried to make a date picker with the following code but nothing append :所以我安装了 jquery.ui.combined,放置了每个(可能太多)render 或<script>并且我尝试使用以下代码制作一个日期选择器,但没有附加任何内容:

<script>
      $(function() {
        $( "#datepicker" ).datepicker( $.datepicker.regional[ "fr" ] );
        $( "#locale" ).change(function() {
          $( "#datepicker" ).datepicker( "option",
            $.datepicker.regional[ $( this ).val() ] );
        });
      });
    </script>
     //more code 
    <input type="text" id="datepicker">

Can someone explain why ?有人可以解释为什么吗? Thanks in advance !提前致谢 !

Well here is how I suggest you to do:那么我建议你这样做:

<script>
  $(function() {
    $("#datepicker").datepicker($.datepicker.regional['fr']);
    $("#locale").change(function() {
      var locale = $.datepicker.regional[$(this).val()];
      //destroy previose datapicker
      $("#datepicker").datepicker("destroy");
      //init new one
      $("#datepicker").datepicker(locale);
    });
  });

</script>

<select id="locale">
  <option val="fr">fr</option>
  <option val="en">en</option>
  <option val="ru">ru</option>
  <option val="de">de</option>
</select>
<input type="text" id="datepicker">

I created jsFiddle example from yours.我从您的示例中创建了jsFiddle 示例

Note that you should add this i18n script to make it work:请注意,您应该添加此 i18n 脚本以使其工作:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/i18n/jquery-ui-i18n.min.js"></script>

You should add to your head this:你应该在你的head加上:

<script type="text/javascript" src="//code.jquery.com/jquery-1.9.1.js"></script><style type="text/css"></style>
<link rel="stylesheet" type="text/css" href="https://ajax.aspnetcdn.com/ajax/jquery.ui/1.9.2/themes/black-tie/jquery-ui.css">
<script type="text/javascript" src="https://ajax.aspnetcdn.com/ajax/jquery.ui/1.9.2/jquery-ui.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/i18n/jquery-ui-i18n.min.js"></script>

Im assuming that you got your answer above.我假设你得到了上面的答案。 However its awlays a best pracice to have script in a js file.然而,最好的做法是将脚本放在 js 文件中。

Lets say you create a js file like so:假设您像这样创建一个 js 文件:

$.myApplication = {

  init: function(){

        $( "#datepicker" ).datepicker( $.datepicker.regional[ "fr" ] );

        $( "#locale" ).change(function() {
        $( "#datepicker" ).datepicker( "option",
           $.datepicker.regional[ $( this ).val() ] );
        });     
  }

}

The view where you want to execute the script:要执行脚本的视图:

@section Scripts {
    <script type="text/javascript">
        $(function () {
            $.myApplication.init();
        })
    </script>
    }

In your bundleconfig:在您的 bundleconfig 中:

bundles.Add(new ScriptBundle("~/bundles/CustomScripts").Include(
                  "~/Scripts/MyApp.js"));

layout:布局:

 @Scripts.Render("~/bundles/CustomScripts")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM