我有输入为“datapicker”的表单...令人恼火的是,我看不到输入错误,因为输入具有"readonly“属性。
所以我有这样的代码:
$('p.send-app-btn input[type="submit"]').on('click', function(){
$('input').each(function(){
if ( $(this).is('[readonly]') && $(this).prop('required') && !$(this).val()) {
$(this).removeAttr('readonly');
if($(this).is(':focus') && $(this).hasClass('hasDatepicker')){
setTimeout(function () {
$(this).attr('readonly','readonly');
}, 1500);
}
}
})
});
而且它工作得很好,因为它从$(this)
输入中删除了属性"readonly“,并且显示了输入错误。但我希望在1.5秒之后(在错误消失之后),聚焦的输入将再次成为“只读”。
但是这部分代码无法工作,因为我无法捕获$(this)
--我如何才能用另一种方式来实现呢?
if($(this).is(':focus') && $(this).hasClass('hasDatepicker')){
setTimeout(function () {
$(this).attr('readonly','readonly');
}, 1500);
}
我希望这是清楚的。:)
发布于 2018-08-26 21:50:55
通过help @Ulrich-dohou,我找到了解决方案:
$('input').each(function(){
if ( $(this).is('[readonly]') && $(this).prop('required') && !$(this).val()) {
$(this).removeAttr('readonly');
setTimeout(() => {
$(this).attr('readonly','readonly');
},1500)
}
})
只是我的代码在按下提交按钮的瞬间检查了$(this).is(':focus')
。这是足够晚一点检查它(足够1秒),它工作得很好。感谢您的回答!
发布于 2018-08-26 21:27:14
您可以将this
作为参数传递给setTimeout
函数-
setTimeout(function(_this) {
_this.attr('readonly','readonly');
}, 1500, $(this));
https://stackoverflow.com/questions/52026630
复制相似问题