单击数字微调器后更新数据库 - php

时间:2015-09-23 03:21:41

标签: javascript php jquery twitter-bootstrap

我有自带输入文本和按钮的数字微调器。这是代码:

<div class="input-group">
     <span class="input-group-btn">
          <button type="button" class="btn btn-default btn-number btn-sm" id="<?=$idBarang?>" disabled="disabled" data-type="minus" data-field="quant[<?=$i++?>]">
               <span class="glyphicon glyphicon-minus"></span>
          </button>
     </span>

     <input type="text" name="quant[<?=$j++?>]" class="form-control input-number input-sm" style="text-align:center;"  value="<?=$jumlah?>" min="1" max="10">

     <span class="input-group-btn">
          <button type="button" class="btn btn-default btn-number btn-sm" id="<?=$idBarang?>" data-type="plus" data-field="quant[<?=$k++?>]">
     <span class="glyphicon glyphicon-plus"></span>
          </button>
     </span>
</div>

我有这样的js文件:

var urlTroli = "edit_troli.php";

    $('.btn-number').click(function(e){
    e.preventDefault();

    id_pro = this.id;
    fieldName = $(this).attr('data-field');
    type      = $(this).attr('data-type');
    var input = $("input[name='"+fieldName+"']");
    var currentVal = parseInt(input.val());
    if (!isNaN(currentVal)) {
        if(type == 'minus') {
            newVal = currentVal -1;
            if(currentVal > input.attr('min')) {
                input.val(newVal).change();
            } 
            if(parseInt(input.val()) == input.attr('min')) {
                $(this).attr('disabled', true);
            }

        } else if(type == 'plus') {
            newVal = currentVal +1;
            if(currentVal < input.attr('max')) {
                input.val(newVal).change();
                $.post(urlTroli, {idPro: id_pro, jml:newVal} ,function() {
                window.location.replace("edit_troli.php"); 
                });
            }
            if(parseInt(input.val()) == input.attr('max')) {
                $(this).attr('disabled', true);
            }
        }
    } else {
        input.val(0);
    }

});
$('.input-number').focusin(function(){
   $(this).data('oldValue', $(this).val());
});
$('.input-number').change(function() {

    minValue =  parseInt($(this).attr('min'));
    maxValue =  parseInt($(this).attr('max'));
    valueCurrent = parseInt($(this).val());

    name = $(this).attr('name');
    if(valueCurrent >= minValue) {
        $(".btn-number[data-type='minus'][data-field='"+name+"']").removeAttr('disabled')
    } else {
        alert('Sorry, the minimum value was reached');
        $(this).val($(this).data('oldValue'));
    }
    if(valueCurrent <= maxValue) {
        $(".btn-number[data-type='plus'][data-field='"+name+"']").removeAttr('disabled')
    } else {
        alert('Sorry, the maximum value was reached');
        $(this).val($(this).data('oldValue'));
    }


});
$(".input-number").keydown(function (e) {
        // Allow: backspace, delete, tab, escape, enter and .
        if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 190]) !== -1 ||
             // Allow: Ctrl+A
            (e.keyCode == 65 && e.ctrlKey === true) || 
             // Allow: home, end, left, right
            (e.keyCode >= 35 && e.keyCode <= 39)) {
                 // let it happen, don't do anything
                 return;
        }
        // Ensure that it is a number and stop the keypress
        if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
            e.preventDefault();
        }
    });

我发布的代码只要点击加号按钮就会发布值,它会将值发布到edit_troli.php。每次我点击加号按钮几次,页面总是重新加载到troli.php,只要点击我已经完成,如何点击微调器时重新加载/发布一次?

第二个问题是,当我将post目标更改为edit_troli.php时,我在idPro和jml上得到了未定义。任何人都可以解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

这是给你的第一个问题,我没试过,但我认为它会起作用

var urlTroli = "edit_troli.php";
var reloadPageNow;
    $('.btn-number').click(function(e){
    e.preventDefault();

    if(reloadPageNow)
         clearTimeout(reloadPageNow );


    id_pro = this.id;
    fieldName = $(this).attr('data-field');
    type      = $(this).attr('data-type');
    var input = $("input[name='"+fieldName+"']");
    var currentVal = parseInt(input.val());
    if (!isNaN(currentVal)) {
        if(type == 'minus') {
            newVal = currentVal -1;
            if(currentVal > input.attr('min')) {
                input.val(newVal).change();
            } 
            if(parseInt(input.val()) == input.attr('min')) {
                $(this).attr('disabled', true);
            }

        } else if(type == 'plus') {
            newVal = currentVal +1;
            if(currentVal < input.attr('max')) {
                input.val(newVal).change();
                $.post(urlTroli, {idPro: id_pro, jml:newVal} ,function() {
                 reloadPageNow = setTimeout(function()
                 {
                     window.location.replace("edit_troli.php"); 
                 },500);
                });
            }
            if(parseInt(input.val()) == input.attr('max')) {
                $(this).attr('disabled', true);
            }
        }
    } else {
        input.val(0);
    }

});
$('.input-number').focusin(function(){
   $(this).data('oldValue', $(this).val());
});
$('.input-number').change(function() {

    minValue =  parseInt($(this).attr('min'));
    maxValue =  parseInt($(this).attr('max'));
    valueCurrent = parseInt($(this).val());

    name = $(this).attr('name');
    if(valueCurrent >= minValue) {
        $(".btn-number[data-type='minus'][data-field='"+name+"']").removeAttr('disabled')
    } else {
        alert('Sorry, the minimum value was reached');
        $(this).val($(this).data('oldValue'));
    }
    if(valueCurrent <= maxValue) {
        $(".btn-number[data-type='plus'][data-field='"+name+"']").removeAttr('disabled')
    } else {
        alert('Sorry, the maximum value was reached');
        $(this).val($(this).data('oldValue'));
    }


});
$(".input-number").keydown(function (e) {
        // Allow: backspace, delete, tab, escape, enter and .
        if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 190]) !== -1 ||
             // Allow: Ctrl+A
            (e.keyCode == 65 && e.ctrlKey === true) || 
             // Allow: home, end, left, right
            (e.keyCode >= 35 && e.keyCode <= 39)) {
                 // let it happen, don't do anything
                 return;
        }
        // Ensure that it is a number and stop the keypress
        if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
            e.preventDefault();
        }
    });