javascript – 从chrome中输入HTML5输入[type =“date”]的输入值

前端之家收集整理的这篇文章主要介绍了javascript – 从chrome中输入HTML5输入[type =“date”]的输入值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在chrome中,标签就像
<input id="picker" type="date">

呈现为文本字段.然而,呼吁尝试用类似的东西来获得它的价值

$("#picker").val()

在输入有效日期或从其下拉列表中选择有效日期之前,不会返回任何内容.
我看了一下按键的所有对象的直接属性

$("#picker").keypress(function() {
    var output = ""
    for (var i in this) {       
        output += i +" value:"+ this[i] + "\n";
    }
    alert(output);
});​

但无法看到我在这些方面的意见.请在http://jsfiddle.net/5cN2q/自行检查

我的问题是:当输入不是有效日期时,是否可以从chrome中输入[type =“date”]获取文本?

解决方法

W3C Date State spec定义了清理算法:
  • The value sanitization algorithm is as follows: If the value of the element is not a valid date string,then set it to the empty string instead.

每当设置input元素的值时调用,如DOM input value spec中所定义:

  • On getting,it must return the current value of the element. On setting,it must set the element’s value to the new value,set the
    element’s dirty value flag to true,invoke the value sanitization
    algorithm
    ,if the element’s type attribute’s current state defines
    one,and then,if the element has a text entry cursor position,should
    move the text entry cursor position to the end of the text field,
    unselecting any selected text and resetting the selection direction to
    none.

因此,当日期无效时,value属性将始终为空字符串.似乎没有指定“原始/脏/用户类型文本值”的任何属性,因为它毕竟不是文本输入.

IMO这是一件好事,它有助于表单验证,因为无效日期被视为虚假值(空字符串),它还使浏览器更自由地实现日期输入的UI.

如果您更喜欢非W3C日期输入,则可以使用具有良好旧JS日期验证的文本输入和日期选择器,例如jQuery UI datepicker.这将允许您检索输入的实际文本值,并且更多跨浏览器解决方案.

原文链接:https://www.f2er.com/js/240840.html

猜你在找的JavaScript相关文章