如何在文档准备好后在 .live() 内运行代码?
$('#foo').live('keyup', function (e) {
var input = $(this).val();
// code to process input
});
这是在帖子表单中使用的,当文档准备好时,我需要在 live() 中运行代码。除了等待按键之外,还有其他方法可以调用它吗?
$('#foo').live('keyup', function (e) {
var input = $(this).val();
// code to process input
});
This is used in a post form and I need to run the code inside the live() when the document is ready. Is there a way, other than to wait for a key press, to invoke it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
听起来您好像在说,有一部分代码将在
keyup
上运行,您也希望在页面加载时运行一次。如果是这种情况,您可以将该代码放入另一个函数中,并在页面加载时和
.live()
处理程序中调用它。It sounds like you're saying that there's a part of the code that will run on
keyup
that you also want to run once when the page loads.If that's the case, you can place that code inside another function, and call it both on page load and inside the
.live()
handler.如果您询问如何在文档准备好之后附加 keyup 事件,这就是解决方案:
但是,这没有任何功能,因为整个想法都是
jQuery.live< /code> 是自动绑定到每个选择器,现在和将来。
此外,您还指定一个 ID 作为选择器。 id 对于元素来说必须是唯一的。
因此,能够将其绑定到 ID 为 foo 的多个元素在技术上是不正确的。
If you're asking how to attach the keyup event after the document is ready, this is the sollution:
However, this would have no function, because the whole idea of heaving
jQuery.live
is to automaticly bind to each selector, now and in the future.Also, you're specifing an ID as the selector. The id must be unique to an element.
Thus being able to bind it to multiple elements with the ID foo would be technically incorrect.