简体   繁体   English

如何防止重复提交<a href=“”>?</a>

[英]How can i prevent double submission of <a href=“”>?

I have a question regarding the double submission. 我对双重提交有疑问。

I have a multiple <a href = ""> . 我有多个<a href = ""> I want to disables all the <a href=""> if i click in one of the <a href= ""> 如果我单击<a href= ""> <a href="">

Code: 码:

<a href="dashboard.php" id ="submitID" class="submit" >Dashboard </a>
<a href="orderList.php"  id ="submitID" class="submit" >Order List</a>
<a href="newOrder.php"  id ="submitID" class ="submit">New Order</a>

First, please fix your id s to be unique. 首先,请修改您的id以使其唯一。

If you're using jQuery versions 1.4.3+: 如果您使用的是jQuery 1.4.3+版本:

$("a.submit").click(function() {
    $("a.submit").bind('click', false);
});

If not, bind function() { return false; } 如果不是,则绑定function() { return false; } function() { return false; } . function() { return false; } Then you can also 那你也可以

$("a.submit").unbind('click')

when you want them to work again. 当您希望他们再次工作时。

Welcome to Stack Overflow. 欢迎使用堆栈溢出。

First of all, you should never have multiple DOM elements with the same ID. 首先,永远不要有多个具有相同ID的DOM元素。

Second of all, set a variable in a bind to the submit class (the bind is using jquery), and flip it if you submit. 第二,在绑定中为提交类设置一个变量(该绑定使用jquery),如果您提交则翻转它。

Include jquery with a script tag and then wrap your javascript in document ready 包含带有脚本标签的jquery,然后将javascript包装到文档中

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<script type="text/javascript>
$(document).ready(function () {
    $('.submit').bind('click', function () {
        var isSubmitted = false;
        if (isSubmitted === false) {
            $.get($(this).attr('href'), function () {
                isSubmitted = true;
            });
        }
    });
});
</script>

This is of course assuming you want some ajax style functionality. 当然,这是假设您需要一些ajax样式的功能。 If not, you shouldn't really be worried if you have a link since you'd be posting to a new page 如果没有,则不必担心是否有链接,因为您将发布到新页面

Jquery: jQuery:

var count=0;
$(".submit").click(function(){
if(count>0){
return false
}
++count;
});

HTML 的HTML

<a href="dashboard.php" id ="submitID1" class="submit" >Dashboard </a>

<a href="orderList.php"  id ="submitID2" class="submit" >Order List</a>    

<a href="newOrder.php"  id ="submitID3" class ="submit">New Order</a>
var submitStatus = false;
    $('a.submit').click(function(e){
        if (!submitStatus) {
            alert('clicked');
            submitStatus = true;
        } else {
            e.preventDefault();
        }
    });

You can try it here: http://jsfiddle.net/p8a5s/ And don t use the same ID s for different DOM elements, of course 您可以在这里尝试: http : //jsfiddle.net/p8a5s/当然 ,不要对不同的DOM元素t use the same ID

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

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