有人可以告诉我 jQuery live 方法有什么问题吗?
我创建了这个简单的时间选择器应用程序,但由于某种原因,这段代码不起作用。我已经成功了(也许这是我的想象)。
我有 4 个单选按钮。每个按钮都附加了一个单击事件(使用 jquery 的 live 方法),我显示一个 DIV 并隐藏其他 DIV。我还(我以为我是)将插件的方法添加到新显示的 DIV 中。
看看下面的链接,您就会明白我的意思。第一个 DIV 有效,但后续的 DIV 无效。我缺少什么?
谢谢!
I've created this simple time picker app but for some reason this snippet of code isn't working. I had it working (maybe it was my imagination).
I have 4 radio buttons. With each button having a click event attached (using jquery's live method), I show a DIV and hide the other DIVs. I also (i thought i was) adding a plugin's method to the newly shown DIV.
Take a look at the link below and you'll see what I mean. The first DIV works, but subsequent ones don't. What am I missing?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码存在很多问题。一方面,有多个文本框带有 id 的
starttime
和endtime
,而 id 在文档中只能出现一次。导致您问题的原因是,单击任何单选按钮时,
timepicker
将为匹配$的所有元素进行初始化("#starttime, #endtime")
选择器。这意味着第一个div
(可见)工作正常,并且选择字段的位置正确,但对于后续div
来说,它被定位在它们关联的 div 被隐藏的时间,因此无法计算它们的位置。我会考虑更改代码,以便您的
div
的 id 称为“recur1”、“recur2”等,而不是“每日”和“每周”,并且还给它们中的所有一个类,“重复”。然后您将能够删除所有 if 语句并执行以下操作:它还将使您能够执行此操作,这将解决您的问题:
There are a bunch of problems with your code. For one thing, there are several textboxes with the id's
starttime
andendtime
, whereas an id should occur only once in the document.What's causing your problem is the fact that upon the click of any radiobutton, the
timepicker
will be initialized for all elements matching the$("#starttime, #endtime")
selector. That will mean the firstdiv
(which is visible), works OK, and the select fields get positioned alright, but for subsequentdiv
's, it is being positioned at a time when the div they're associated with are hidden, and as such, their positions cannot be calculated.I'd consider changing the code so that your
div
's have id's called "recur1", "recur2", etc, rather than "daily" and "weekly", and also give al of them a class, "recur". Then you'll be able to remove all your if-statements and just do:It'll also enable you to do this, which will solve your problem: