dongtang6681 2015-10-17 18:56
浏览 28

检查表单提交的最小和最大数量

Working with an existing script that checks input values on form submission against existence of minimum quantity. I need to include a condition to check for the existence of maximum quantity and display an error message when the condition is not met. I believe it's a matter of editing the existing conditional logic but for the life of me I cannot get the edits to function without hosing up the functioning minimum condition. Rather than submitting my failed attempt at altering the code I'm submitting a functional version that only validates minimum quantities.

$('#button-cart').on('click', function() {
    var total_quantity = 0;
    var min_oneside = parseInt("<?php echo $min_oneside; ?>");
    var min_bothside = parseInt("<?php echo $min_bothside; ?>");
    var min_noprint = parseInt("<?php echo $min_noprint; ?>");
    var min_standard = parseInt(" <?php echo $minimum; ?>");
    var max_standard = parseInt(" <?php echo $maximum; ?>");
    var validQty = true;
    var validMaxQty = true;
    var validInput = true;      
    var oth_minimum = "Quantity must be at least <?php echo $minimum; ?>";
    var oth_maximum = "Quantity must be less than <?php echo $maximum; ?>"

    $('#content').find('input[name^="option-quantity"]').each(function(){
            total_quantity = total_quantity + parseInt($(this).val());
    });
    $('#content').find('input[name="quantity"]').each(function(){
            total_quantity = total_quantity + parseInt($(this).val());
    });

    var element = $("label:contains('Printing')").attr('for');
    if(element === undefined){
        if( total_quantity < min_standard ) {
            validQty = false;
            validInput = false;
            oth_minimum = "Quantity must be at least " + min_standard;
        }else if (total_quantity > max_standard){
            validQty = false;
            validInput = false;
            oth_message = oth_maximum;
            }
    } else {
        if ( ($("#" + element + " option:selected").text()).match("Printed One Side") ){
            if( total_quantity < min_oneside ) {
                validQty = false;
                validInput = false;
                oth_minimum = "Quantity must be at least " + min_oneside;
            }else if (total_quantity > max_standard){
                validQty = false;
                validInput = false;
                oth_message = oth_maximum;
            }
        }
        if ( ($("#" + element + " option:selected").text()).match("Printed Both Sides") ){
            if( total_quantity < min_bothside ) {
                validQty = false;
                validInput = false;
                oth_minimum = "Quantity must be at least " + min_bothside;
            }else if (total_quantity > max_standard){
                validQty = false;
                validInput = false;
                oth_message = oth_maximum;
            }
        }
        if ( ($("#" + element + " option:selected").text()).match("No Printing") ){
            if( total_quantity < min_noprint ) {
                validQty = false;
                validInput = false;
                oth_minimum = "Quantity must be at least " + min_noprint;
            }else if (total_quantity > max_standard){
                validQty = false;
                validInput = false;
                oth_message = oth_maximum;
            }
        }
    }
    if(!validQty) {
        $('#errorQuantity').html(oth_minimum);
    }
    var minimum = "Quantity must be at least <?php echo $minimum; ?>";
    if($('#errorQuantity').length > 0 && validQty) {
        var quantity = 0;
        $('#errorQuantity').empty();
        $('input[type=number]').each(function () {
            var currentId = $(this).attr('id');
            checkboxId = currentId.replace("quantity-", "");
            if(isNaN($(this).val()) || $(this).val() < 0) {
                $(this).focus();
                $(this).css('background-color', 'red');
                validInput = false;
            } else {
                quantity += parseInt($(this).val());
            }
        });
        if(quantity <= 0) {
            $('#errorQuantity').append(minimum);
            validInput = false;
        }
    }
    if(validInput){
        $('input[type=number]').each(function () {
            $(this).css('background-color', '#FFFFFF');
        });
        $('#errorQuantity').empty();

        $.ajax({
            url: 'index.php?route=checkout/cart/add',
            type: 'post',
            data: $('.product-info input[type=\'text\'], .product-info input[type=\'number\'], .product-info input[type=\'date\'], .product-info input[type=\'datetime\'], .product-info input[type=\'hidden\'], .product-info input[type=\'radio\']:checked, .product-info input[type=\'checkbox\']:checked, .product-info select, .product-info textarea'),
            dataType: 'json',
            beforeSend: function() {
                $('#button-cart').attr('disabled', true);
                $('#button-cart').after('<i class="fa fa-circle-o-notch fa-spin"></i>');
            },
            success: function(json) {
                $('.alert, .text-danger').remove();
                $('.form-group').removeClass('has-error');
                $('#button-cart').next('.fa-spin').remove();
                $('#button-cart').attr('disabled', false);
                if (json['error']) {
                    var errors = '';
                    if (json['error']['option']) {
                        for (i in json['error']['option']) {
                            var element = $('#input-option' + i.replace('_', '-'));
                            element.parents('.form-group').first().find('> label + div').append('<div class="text-danger">' + json['error']['option'][i] + '</div>');
                        }
                    }
                if (json['error']['recurring']) {
                    $('select[name="recurring_id"]').after('<span class="error">' + json['error']['recurring'] + '</span>');
                }
                // Highlight any found errors
                $('.text-danger').each(function() {
                    $(this).parents('.form-group').first().addClass('has-error');
                });
            }
            if (json['success']) {
                $('.tb_widget_cart > .tb_nav').load('index.php?route=common/cart/info .tb_nav > *');
                window.location = $('base').attr('href') + 'index.php?route=checkout/cart';
                //displayNotice('product', 'success', 'product', json['success']);
            }
        }
    });
}

});

  • 写回答

1条回答 默认 最新

  • dongqu5650 2015-10-17 19:24
    关注

    Should be something like :

        if(element === undefined){
        if( total_quantity < min_standard ) {
            validQty = false;
            validInput = false;
            oth_message = oth_minimum;
        }else if (total_quantity > max_standard){
            validQty = false;
            validInput = false;
            oth_message = oth_maximum;
        }
    } else {
        if ( ($("#" + element + " option:selected").text()).match("Printed One Side") ){
            if( total_quantity < min_oneside ) {
                validQty = false;
                validInput = false;
                oth_message = "Quantity must be at least " + min_oneside;
            }
        }
        if ( ($("#" + element + " option:selected").text()).match("Printed Both Sides") ){
            if( total_quantity < min_bothside ) {
                validQty = false;
                validInput = false;
                oth_message = "Quantity must be at least " + min_bothside;
            }
        }
        if ( ($("#" + element + " option:selected").text()).match("No Printing") ){
            if( total_quantity < min_noprint ) {
                validQty = false;
                validInput = false;
                oth_message = "Quantity must be at least " + min_noprint;
            }
        }
    }
    if(!validQty) {
        $('#errorQuantity').html(oth_message);
    }
    

    You can add in the same way for the other conditionals, I added only for the first one.

    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法