简体   繁体   English

jQuery easyconfirm插件仅在循环的第一次迭代中起作用

[英]jQuery easyconfirm plugin only working on first iteration of loop

I am using the easyconfirm dialog plugin found here - http://www.projectshadowlight.org/jquery-easy-confirm-dialog/ 我正在使用在此处找到的easyconfirm对话框插件-http: //www.projectshadowlight.org/jquery-easy-confirm-dialog/

I am also using a classic asp do while loop to print the that it attached to, see sample below; 我还使用经典的asp do while循环来打印其附加的内容,请参见下面的示例;

<% Do while not rs.EOF %>
    <a href="#" id="yesno">This link</a>
<% rs.MoveNext()
Loop %>

You get the idea. 你明白了。

My Jquery for this is straight out of the docs; 我的Jquery就是直接来自文档;

$("#yesno").easyconfirm({locale: { title: 'Are you sure?', button: ['No','Yes']}});

$("#yesno").click(function() {
    alert("You clicked yes");
});

The problem i am having is that the dialog is only working on the first link in the loop, if its returning more than 1, then the first work but the rest dont and it doesnt throw any errors, it just adds the # to the URL 我遇到的问题是,对话框仅在循环中的第一个链接上工作,如果返回的结果大于1,则第一个工作,但其余部分不工作,并且不引发任何错误,它仅将#添加到URL

Any help would be awesome :) 任何帮助都是极好的 :)

The thing and the problem here is that you have the same id for the different links, you need to give them different id's or give them a class attribute, for example: 这里的问题是,对于不同的链接,您具有相同的ID,您需要为它们提供不同的ID或为它们提供类属性,例如:

<% Do while not rs.EOF %>
    <a href="#" class="yesno">This link</a>
<% rs.MoveNext()
Loop %>

This will be your new js, take into account that we are putting in the selector the "a" elements with the class yesno. 考虑到我们将选择器中带有类yesno的“ a”元素放入选择器,这将是您的新js。

$("a.yesno").easyconfirm({locale: { title: 'Are you sure?', button: ['No','Yes']}});

$("a.yesno").click(function() {
    alert("You clicked yes");
});

Looking at your source code it seems that you're binding your easyconfirm action to a DOM element through #yesno id. 查看您的源代码,似乎您正在通过#yesno id将easyconfirm操作绑定到DOM元素。 Now, since your list could contain more than one element, this bind by id will work just on one of them since identifiers should be unique. 现在,由于您的列表可能包含多个元素,因此ID绑定仅对其中之一起作用,因为标识符应该是唯一的。 My suggestion then, is to use a CSS class selector. 那么我的建议是使用CSS类选择器。

I'd go with something like: 我会选择类似的东西:

<% Do while not rs.EOF %>
    <a href="#" class="yesno">This link</a>
<% rs.MoveNext()
Loop %>

and

$(".yesno").easyconfirm({locale: { title: 'Are you sure?', button: ['No','Yes']}});

$(".yesno").click(function() {
    alert("You clicked yes");
});

So, basically, the jQuery binding here will go through the CSS class selector. 因此,基本上,这里的jQuery绑定将通过CSS类选择器进行。

You can't have more than one elements in your DOM with the same id. 相同ID的DOM中不能有多个元素。 Use classes instead 使用类代替

<a href="#" class="yesno">This link</a>


$(".yesno").each(function() {
    $(this).easyconfirm({locale: { title: 'Are you sure?', button: ['No','Yes']}});
    $(this).click(function() {
        alert("You clicked yes");
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM