简体   繁体   English

Click事件未在由Html.ActionLink生成的链接上触发

[英]Click event not firing on link generated with Html.ActionLink

I have an ActionLink as below: 我有一个ActionLink,如下所示:

@Html.ActionLink("-Pg", "SupprimerPage", "Section", 
new { pageId = @item.Id }, new { @id = "ConfirmDeletePage", @class = "editLink", style 
= "width:30px" })

And my script: 而我的脚本:

  $(document).ready(function () {
    $(document).on('click', '#ConfirmDeletePage', function () {
        var x=confirm("Confirm delete page?");
        console.log(x);
        if (x == true){
            return true;
        }
        else {
            return false;
        }
    });
});

When I use a hardcoded link with the <a> tag, it works fine. 当我在<a>标记中使用硬编码链接时,它可以正常工作。 But when I try to generate the link using Html.ActionLink , the event handler is not called. 但是,当我尝试使用Html.ActionLink生成链接时,未调用事件处理程序。 any help? 有什么帮助吗?

For your problem "when I click on Yes, the method in the ActionLink is not being called" this is because you are returning yes on yes button so after execute your function it follow link "href" that's why it seems the method in the ActionLink is not being called, just return false or use event.preventDefault(); 对于您的问题“当我单击“是”时,没有调用ActionLink中的方法”,这是因为您在“是”按钮上返回了“是”,因此在执行功能后,它会跟随链接“ href”,这就是在ActionLink中似乎该方法的原因不被调用,仅返回false或使用event.preventDefault(); inside of true section which run on press yes button 在运行“是”按钮的真实部分的内部

  $(document).ready(function () { $(document).on('click', '.editLink', function (event) { var x=confirm("Confirm delete page?"); console.log(x); if (x == true){ return true; } else { alert('Pressed NO') return false; } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script> <a href="http://hello.com" class="editLink" >Click me !</a> 

for more go with this anchor-tag-with-javascript-onclick-event 有关更多信息,请使用此anchor-tag-with-javascript-onclick-event

If it works with a hardcoded link, but not when you use an ActionLink, that means the ActionLink must be generating something different to your hardcoded link, which means your jQuery selector ( #ConfirmDeletePage ) is not finding the element. 如果它适用于硬编码链接,但不适用于使用ActionLink的链接,则表示ActionLink必须生成与硬编码链接不同的内容,这意味着jQuery选择器( #ConfirmDeletePage )找不到元素。

You can confirm this by using your browser's developer tools to inspect the <a... tag generated by the ActionLink. 您可以通过使用浏览器的开发人员工具检查ActionLink生成的<a...标签来确认这一点。

Based on the question Html.ActionLink with a specified HTML id maybe you need to change @id = "ConfirmDeletePage" to id = "ConfirmDeletePage" . 基于具有指定HTML id的Html.ActionLink问题也许您需要将@id = "ConfirmDeletePage"更改为id = "ConfirmDeletePage"

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

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