Home >Web Front-end >JS Tutorial >How to use jquery date control

How to use jquery date control

(*-*)浩
(*-*)浩Original
2019-06-01 16:12:253171browse

JQuery is a very excellent scripting framework. Its rich controls are very simple to use and the configuration is very flexible. Below is an example of using the date plug-in datapicker.

How to use jquery date control

Needless to say, download the jQuery core file. Datepicker is a lightweight plug-in. You only need the min version of jQuery. Then go to the official website to download the jquery-ui compressed package. (You can choose your favorite theme), which includes support for datepicker. Of course, you can also download datepicker from the official website, including ui.core.js and ui.datepicker.js.

Recommended courses: jQuery Tutorial.

jquery.ui.datepicker-zh-CN.js, the content is as follows:

jQuery(function($){
    $.datepicker.regional['zh-CN'] = {
        closeText: '关闭',
        prevText: '<上月&#39;,
        nextText: &#39;下月>',
        currentText: '今天',
        monthNames: ['一月','二月','三月','四月','五月','六月',
        '七月','八月','九月','十月','十一月','十二月'],
        monthNamesShort: ['一','二','三','四','五','六',
        '七','八','九','十','十一','十二'],
        dayNames: ['星期日','星期一','星期二','星期三','星期四','星期五','星期六'],
        dayNamesShort: ['周日','周一','周二','周三','周四','周五','周六'],
        dayNamesMin: ['日','一','二','三','四','五','六'],
        weekHeader: '周',
        dateFormat: 'yy-mm-dd',
        firstDay: 1,
        isRTL: false,
        showMonthAfterYear: true,
        yearSuffix: '年'};
    $.datepicker.setDefaults($.datepicker.regional['zh-CN']);
});

Instance:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <TITLE>日期控件datepicker</TITLE>
    
    <!-- 引入 jQuery -->
    <mce:script src="js/jquery.1.4.2.js" mce_src="js/jquery.1.4.2.js" type="text/javascript"></mce:script>

    <!--添加datepicker支持-->
    <mce:script src="js/jquery.ui.core.js" mce_src="js/jquery.ui.core.js" type="text/javascript"></mce:script>
    <mce:script src="js/jquery.ui.datepicker.js" mce_src="js/jquery.ui.datepicker.js" type="text/javascript"></mce:script>
    <!-- 或者引入jquery ui包,其中也包含对datepicker的支持
    <mce:script src="js/jquery-ui-1.7.3.custom.min.js" mce_src="js/jquery-ui-1.7.3.custom.min.js" type="text/javascript"></mce:script>
    -->

    <!--引入样式css-->
    <link type="text/css" rel="stylesheet" href="css/jquery-ui-1.7.3.custom.css" mce_href="css/jquery-ui-1.7.3.custom.css" />

    <!-- 添加中文支持-->
    <mce:script src="js/jquery.ui.datepicker-zh-CN.js" mce_src="js/jquery.ui.datepicker-zh-CN.js" type="text/javascript"></mce:script>

    <mce:script type="text/javascript"><!--
    //等待dom元素加载完毕.
        $(function(){
            $("#selectDate").datepicker({//添加日期选择功能
            numberOfMonths:1,//显示几个月
            showButtonPanel:true,//是否显示按钮面板
            dateFormat: &#39;yy-mm-dd&#39;,//日期格式
            clearText:"清除",//清除日期的按钮名称
            closeText:"关闭",//关闭选择框的按钮名称
            yearSuffix: &#39;年&#39;, //年的后缀
            showMonthAfterYear:true,//是否把月放在年的后面
            defaultDate:&#39;2011-03-10&#39;,//默认日期
            minDate:&#39;2011-03-05&#39;,//最小日期
            maxDate:&#39;2011-03-20&#39;,//最大日期
            //monthNames: [&#39;一月&#39;,&#39;二月&#39;,&#39;三月&#39;,&#39;四月&#39;,&#39;五月&#39;,&#39;六月&#39;,&#39;七月&#39;,&#39;八月&#39;,&#39;九月&#39;,&#39;十月&#39;,&#39;十一月&#39;,&#39;十二月&#39;],
            //dayNames: [&#39;星期日&#39;,&#39;星期一&#39;,&#39;星期二&#39;,&#39;星期三&#39;,&#39;星期四&#39;,&#39;星期五&#39;,&#39;星期六&#39;],
            //dayNamesShort: [&#39;周日&#39;,&#39;周一&#39;,&#39;周二&#39;,&#39;周三&#39;,&#39;周四&#39;,&#39;周五&#39;,&#39;周六&#39;],
            //dayNamesMin: [&#39;日&#39;,&#39;一&#39;,&#39;二&#39;,&#39;三&#39;,&#39;四&#39;,&#39;五&#39;,&#39;六&#39;],
            onSelect: function(selectedDate) {//选择日期后执行的操作
                alert(selectedDate);
            }
            });
        });
    
// --></mce:script>
 </HEAD>
 <BODY>
  <input type="text" id="selectDate" readonly="readonly"/>
 </BODY>
</HTML>

Note: Since jquery datepicker is not the latest version, downloading the css file in the new version jquery-ui-1.8.13 will cause the date control to be unable to be displayed, so the ui of 1.7.3 is used here. The simpler way is to use jquery-ui to compress js.

The above is the detailed content of How to use jquery date control. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:How to use jquery ajaxNext article:How to use jquery ajax

Related articles

See more