Jquery 在一定时间后对$(this)元素执行某些操作
我有输入“数据采集器”的表单。。。。令人恼火的是,我没有看到输入错误,因为输入具有“readonly”属性 所以我有这个代码:Jquery 在一定时间后对$(this)元素执行某些操作,jquery,html,Jquery,Html,我有输入“数据采集器”的表单。。。。令人恼火的是,我没有看到输入错误,因为输入具有“readonly”属性 所以我有这个代码: $('p.send-app-btn input[type="submit"]').on('click', function(){ $('input').each(function(){ if ( $(this).is('[readonly]') && $(this).prop('required') && !$(this).val())
$('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);
}
}
})
});
它工作得很好,因为它从$(这个)
输入中删除了属性“readonly”,并且显示了输入错误。但我希望在1.5s后(错误消失后),集中输入将再次成为“只读”
但是这部分代码不起作用,因为我无法捕获$(this)
-我如何用另一种方法来实现它
if($(this).is(':focus') && $(this).hasClass('hasDatepicker')){
setTimeout(function () {
$(this).attr('readonly','readonly');
}, 1500);
}
我希望这是清楚的 您只需将$(this)
添加到变量中,或者如果您的浏览器支持ES6,则只需使用箭头功能。另一个例子是,您创建了一个函数来为您处理操作
示例1
if($(this).is(':focus') && $(this).hasClass('hasDatepicker')){
const input = $(this);
setTimeout(function () {
input.attr('readonly', true);
}, 1500);
}
示例2
if($(this).is(':focus') && $(this).hasClass('hasDatepicker')){
setTimeout(() => {
$(this).attr('readonly', true);
}, 1500);
}
示例3
function disabeInput(element){
element.attr('readonly', true);
}
if($(this).is(':focus') && $(this).hasClass('hasDatepicker')){
setTimeout(() => {
disabeInput($(this));
}, 1500);
}
您只需将$(this)
添加到变量中,或者如果您的浏览器支持ES6,则只需使用箭头功能。另一个例子是,您创建了一个函数来为您处理操作
示例1
if($(this).is(':focus') && $(this).hasClass('hasDatepicker')){
const input = $(this);
setTimeout(function () {
input.attr('readonly', true);
}, 1500);
}
示例2
if($(this).is(':focus') && $(this).hasClass('hasDatepicker')){
setTimeout(() => {
$(this).attr('readonly', true);
}, 1500);
}
示例3
function disabeInput(element){
element.attr('readonly', true);
}
if($(this).is(':focus') && $(this).hasClass('hasDatepicker')){
setTimeout(() => {
disabeInput($(this));
}, 1500);
}
您可以将
此
作为参数传递给设置超时
功能-
setTimeout(function(_this) {
_this.attr('readonly','readonly');
}, 1500, $(this));
您可以将
此
作为参数传递给设置超时
功能-
setTimeout(function(_this) {
_this.attr('readonly','readonly');
}, 1500, $(this));
借助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')
。这是一个足够的检查它有点晚(足够1s),它的工作情况良好。谢谢你的回答 借助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')
。这是一个足够的检查它有点晚(足够1s),它的工作情况良好。谢谢你的回答 你好,谢谢你的回答!我都试过了,但任何例子都不起作用。。他们没有为此输入设置属性('readonly')。您好,谢谢您的回答!我都试过了,但任何例子都不起作用。。他们没有为此输入设置属性('readonly')。您好,谢谢您的帮助。我尝试过,但它没有为此输入设置属性…:/你好,谢谢你的帮助。我尝试过,但它没有为此输入设置属性…:/你听说过变量吗?!不这是什么意思?你听说过变量吗?!不这是什么意思?