You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在jQuery中检测输入字段的类型是否为date类型

Fix for Detecting Date Input Types in jQuery

The problem here is that jQuery doesn’t include a built-in :date pseudo-selector like it does for :text. That’s why your check for :text works reliably, but :date fails to trigger as expected.

Instead of relying on a non-existent pseudo-selector, you can directly check the type attribute (or DOM property) of the input element to identify date fields. Here’s how to adjust your code:

Corrected JavaScript Code

$('.check').on('click', function(e) {
  e.preventDefault();
  $('.field input').each(function(index, value) {
    // Check if the input's type attribute is exactly "date"
    if ($(this).attr('type') === 'date') {
      alert('date-found');
    }
  });
});

Alternatively, you can use the prop() method (since type is a native DOM property) which might be slightly more efficient:

if ($(this).prop('type') === 'date') {
  alert('date-found');
}

Why This Works

jQuery explicitly supports :text to match both <input type="text"> elements and inputs without a type attribute (since their default type is text). However, there’s no equivalent built-in selector for date inputs. By checking the type attribute directly, you bypass this limitation and target the exact input type you need.

内容的提问来源于stack exchange,提问作者GarethTF

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 13:27:50