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

标签 javascript jquery asp.net

我有一个 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/

相关文章:

javascript - 单击子内部 anchor 元素时如何删除父div?

javascript - 当被过滤的数组包含 JSON 数据时,为什么我会收到 'cannot read property ' filter' of undefined' ?

javascript - Flash播放器 "now playing list"不中断当前歌曲

jQuery 循环 ul li

c# - 在 Razor View 中的 ASP.NET WEB API 中获取路由前缀的 URL

javascript - JEST : "Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.", 尽管已经配置

javascript - React Native 组件未使用 componentWillMount 或 componentDidMount 中的异步 redux 操作进行渲染

javascript - 如何将 Github 中的代码添加到我的 Codepen 项目中?

c# - 如何在asp中的按钮内添加图像

asp.net - 如何在VB.net中使用字典?