无法选择 jquery 中的克隆元素
克隆选择元素并增加“名称”属性后,我无法选择克隆的元素。
例如:
HTML
<select name="1"></select>
<select name="1-1"></select> <!-- cloned element -->
<select name="1-2"></select> <!-- cloned element -->
JS
$('[name^="1"]').change(function() {
alert(1);
});
当我更改原始元素时,它工作正常,我收到警报,但是当我更改克隆元素之一时,什么也没有发生。没有错误。
注意:我使用“name”属性而不是 ID,因为提交表单后我需要检索这些克隆的元素值,
这应该有效吗?还是克隆的元素无法选择?
After cloning a select element and incrementing the "name" attribute, I am unable to select the cloned elements.
For example:
HTML
<select name="1"></select>
<select name="1-1"></select> <!-- cloned element -->
<select name="1-2"></select> <!-- cloned element -->
JS
$('[name^="1"]').change(function() {
alert(1);
});
When I change the original element, it works fine I get the alert, but when I change one of the cloned elements, nothing happens. There are no errors.
Note: I am using "name" attribute instead of ID because I need to retrieve these cloned element values once the form is submitted
Should this work?? or is it that the cloned elements cant be selected?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是 live()DOC 的文档链接,这是 < a href="http://api.jquery.com/delegate/" rel="nofollow">delegate()DOC
虽然它的功能与常规事件处理程序不同(它实际上是监听事件文档,因此它在传播时“处理”事件的效率更高一些,因为您为其提供了一个侦听上下文:
Here's the doc link for live()DOC and here's the one for delegate()DOC
Although it functions differently than a regular event handler (it's actually listening for events on the document, so it "handles" the event on propagation. Delegate is a little more efficient because you give it a context to listen on: