如何从datepicker ui获取年份? [英] How to get the year from datepicker ui?

查看:55
本文介绍了如何从datepicker ui获取年份?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有用户选择日期的代码.

我需要获取值年份并将其放在div中.

我该怎么做?

jsFissl演示

许多 代码:

<div class="demo">
<p>Date: <input type="text" id="datepicker"></p>
</div>
<div id="Year"></div>

$("#datepicker").datepicker({
        changeMonth: true,
        changeYear: true

    });

       $("#Year").text($("#datepicker").val()); 

我尝试使用.substring(0, 4),但是我不知道怎么做.

解决方案

这将显示您要提取的年份.

HTML:

<meta charset="utf-8">
    <div class="demo">
    <p>Date: <input type="text" id="datepicker"></p>
    <button>Show year</button>
</div><!-- End demo -->

JS:

$(function() {
    $("#datepicker").datepicker({
        changeMonth: true,
        changeYear: true
    });

    $("button").click(function() {
        var split = $("#datepicker").val().split('/');
        alert(split[2]);
    });
});​

String类的 split 方法将字符串分割并作为数组返回./p>

这完成了您想要的一切. 看看 onSelect 事件.

$(function() {
    $("#datepicker").datepicker({
        changeMonth: true,
        changeYear: true,
        onSelect: function(dateText) { 
            var split = $("#datepicker").val().split('/');
            $("#Year").text(split[2]);
        }
    });
});​

I have code which the user choose date .

I need to to get the value year and put it in div.

How can I do that?

jsFissl Demo

many Thx. the code:

<div class="demo">
<p>Date: <input type="text" id="datepicker"></p>
</div>
<div id="Year"></div>

$("#datepicker").datepicker({
        changeMonth: true,
        changeYear: true

    });

       $("#Year").text($("#datepicker").val()); 

I try to use .substring(0, 4)but I don't kno how.

解决方案

This shows the year you wanted to extract.

HTML:

<meta charset="utf-8">
    <div class="demo">
    <p>Date: <input type="text" id="datepicker"></p>
    <button>Show year</button>
</div><!-- End demo -->

JS:

$(function() {
    $("#datepicker").datepicker({
        changeMonth: true,
        changeYear: true
    });

    $("button").click(function() {
        var split = $("#datepicker").val().split('/');
        alert(split[2]);
    });
});​

The split method of String class divides the string and returns as an array.

EDIT: This does everything you wanted. Take a look at the onSelect event.

$(function() {
    $("#datepicker").datepicker({
        changeMonth: true,
        changeYear: true,
        onSelect: function(dateText) { 
            var split = $("#datepicker").val().split('/');
            $("#Year").text(split[2]);
        }
    });
});​

这篇关于如何从datepicker ui获取年份?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆