gpt4 book ai didi

javascript - 如何在 setTimeout() 函数中使用 $(this) 访问 asp 控件

转载 作者:行者123 更新时间:2023-11-28 07:50:50 25 4
gpt4 key购买 nike

我有一个 HTML 控件列表,我需要将 keyup 事件的监听器绑定(bind)到每个控件。一旦触发,一些其他 Action 应该在一段时间延迟后开始。我正在使用 setTimeout 来实现这一点。

$(".TextBoxClass").each(function () {
$(this).keyup(function () {
alert("Id = " + $(this));
setTimeout(function () {
alert("current Id = " + $(this))
}, 50);
})
})

第一条 alert 消息显示触发 keyup 事件的控件的正确 idsetTimeout 中的第二个 alert 表示 current id = [object][object]

如何在 setTimeout 回调函数中访问控件?

最佳答案

  <div id="controls">
<input class="TextBoxClass" type="text" value="1" id="id-1">
<input class="TextBoxClass" type="text" value="2" id="id-2">
<input class="TextBoxClass" type="text" value="3" id="id-3">
<input class="TextBoxClass" type="text" value="4" id="id-4">
<input class="TextBoxClass" type="text" value="5" id="id-5">
</div>


$('#controls').on('keyup', '.TextBoxClass', function () {
var $this = $(this);

setTimeout(function(){
// alert("current value = " + $this.val());
alert("current element id = " + $this.attr('id'));
}, 50);
});

http://jsbin.com/yisore/1/

http://jsbin.com/yisore/1/edit

关于javascript - 如何在 setTimeout() 函数中使用 $(this) 访问 asp 控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26825975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com