Only want a link to be able to be clicked once

给你一囗甜甜゛ 提交于 2019-12-31 07:25:07

问题


I have a link that opens a new page and I was wondering if there was a way that would make the link only click-able once so the form cannot be submitted numerous times.

<div id="addSection"><a class="addSection" 
    onclick="javascript:writeBookmark(this); var newwin = window.open('<c:url value="/URL_WINDOW"/>', 
    'additional', 'width=400,height=280,toolbar=no,,menubar=no,scrollbars=yes,resizeable=no'); newwin.focus(); return null;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    Add sub

I have JavaScript to do this with buttons but not sure how to do it when I am not dealing with buttons.


回答1:


First, I would move your click handler so that it's not applied inline. Then, I would add some code to the handler to either remove the link itself or replace the click handler with one that pops up a message that the link can only be used once.

Example (using jQuery)

$('.addSection').on('click', function(e) {
    e.preventDefault();
    writeBookmark(this);
    var newwin = window.open('<c:url value="/URL_WINDOW"/>', 
                             'additional',
                             'width=400,height=280,toolbar=no,,menubar=no,scrollbars=yes,resizeable=no');
    newwin.focus();
    $(this).off('click').on('click', function() {
        alert('You've already performed this action');
    });
});


来源:https://stackoverflow.com/questions/21856878/only-want-a-link-to-be-able-to-be-clicked-once

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!