简体   繁体   English

如果条件匹配,则停止执行脚本

[英]Stop script from executing if conditions match

When user clicks a submit button, the script modifies a shopping cart printed on the page. 当用户单击提交按钮时,脚本将修改页面上打印的购物车。 However, if the user clicks it again, it will add the same line's again, and I need to stop it. 但是,如果用户再次单击它,它将再次添加同一行,因此我需要停止它。

So I only want the script to modify the contents of the page only once, and do nothing if the contents have already changed. 因此,我只希望脚本仅修改一次页面内容,如果内容已更改,则什么也不做。

Here's what I've tried: 这是我尝试过的:

$(document).ready(function () {
    $(function () {
        $(".submit").click(function () {
            var a = $('.simpleCart_input').val();
            var a1 = $('.simpleCart_input').val();
            if (a1 == a + " kpl") {
                return false;
            } else {
                $('.itemRow .item-quantity').text(a + " kpl");
            }
            var b = $('.itemRow .item-price').html();
            var b1 = $('.itemRow .item-price').html();
            if (b1 == b + " /kpl") {
                return false;
            } else {
                $('.itemRow .item-price').text(b + " /kpl");
            }
            var c = $('.itemRow .item-total').html();
            var c1 = $('.itemRow .item-total').html();
            if (c1 == "Yhteensä: " + c) {
                return false;
            } else {
                $('.itemRow .item-total').text("Yhteensä: " + c);
            }
            var d = $('.simpleCart_grandTotal').html();
            var d1 = $('.simpleCart_grandTotal').html();
            if (d1 == "Tuotteet yhteensä " + d) {
                return false;
            } else {
                $('.simpleCart_grandTotal').text("Tuotteet yhteensä " + d);
            }
            var data = $('#yhteystiedot').serializeArray();
            data.push({
                name: 'cartContent',
                value: $('#emailedcart').html()
            });
            //alert (data);return false;
            $.ajax({
                type: "POST",
                data: data,
                url: "order/order.php",
                dataType: "html",
                error: function () {
                    alert("Jotakin meni pahasti pieleen! Yritä uudelleen?");
                },
                success: function () {
                    alert("Onnistui");
                }


            });
            return false;

        });
    });
});

This just won't work for me. 这对我不起作用。

Sample 样品

something like that should work: 这样的事情应该工作:

$(document).ready(function () {
    var gDone = false;
    $(".submit").click(function () {
        if (gDone) return false; // already submitted
        gDone = true;
        // ...
    });
});

EDIT: it's more user friendly to deactivate or hide the submit button 编辑:停用或隐藏“提交”按钮更加用户友好

There is no possibility your onclick handler will return false, since you're comparing 2 same elements and expecting 2 different values: 您的onclick处理程序不可能返回false,因为您正在比较2个相同的元素并期望2个不同的值:

var a = $('.simpleCart_input').val();
var a1 = $('.simpleCart_input').val();
if (a1 == a + " kpl") {

... same goes for all rows. ...所有行都一样。 a1 will never be a + " kpl" , since you're doing the same select for both, a and a1 . a1永远不会是a + " kpl" ,因为您对aa1都进行了相同的选择。

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

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