如何在jQuery中检测输入字段的类型是否为date类型
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

