使用jQuery

时间:2016-04-06 11:39:48

标签: javascript php jquery ajax

我正在使用数据库中预定义的“包”更新订单中的数量。更新第一个函数中“packs”下拉列表中的from字段有效,但使用这些值并将它们乘以文本输入值的第二个函数不会更新字段。

此外,在查看控制台输出时,看起来第一个和第二个函数的ajax请求都在数量输入更改时运行。

jQuery(document).ready(function($) {

        $("[id^=packs]").on('change', function() {
        var packname = this.value;
        console.log("packname:"+packname);
        var lineno = this.getAttribute('data-lineno');
        console.log("lineno: "+lineno);
        $.ajax({
            type: "post",
            url: "<?php echo base_url(); ?>products/pack",
            cache: false,
           // ContentType : 'application/json',
            data: {name: packname},
            success: function(json){
            try{
                var obj = jQuery.parseJSON(json);
                console.log(obj);
                console.log("size1: "+obj.packdetail[0].size1);

                    $('#'+lineno+'-size1').attr("value", obj.packdetail[0].size1);
                    $('#'+lineno+'-size2').attr("value", obj.packdetail[0].size15);
                    $('#'+lineno+'-size3').attr("value", obj.packdetail[0].size2);
                    $('#'+lineno+'-size4').attr("value", obj.packdetail[0].size25);
                    $('#'+lineno+'-size5').attr("value", obj.packdetail[0].size3);
                    $('#'+lineno+'-size6').attr("value", obj.packdetail[0].size35);
                    $('#'+lineno+'-size7').attr("value", obj.packdetail[0].size4);
                    $('#'+lineno+'-size8').attr("value", obj.packdetail[0].size45);
                    $('#'+lineno+'-size9').attr("value", obj.packdetail[0].size5);
                    $('#'+lineno+'-size10').attr("value", obj.packdetail[0].size55);
                    $('#'+lineno+'-size11').attr("value", obj.packdetail[0].size6);
                    $('#'+lineno+'-size12').attr("value", obj.packdetail[0].size65);
                    $('#'+lineno+'-size13').attr("value", obj.packdetail[0].size7);
                    $('#'+lineno+'-size14').attr("value", obj.packdetail[0].size75);
                    $('#'+lineno+'-size15').attr("value", obj.packdetail[0].size8);
                    $('#'+lineno+'-size16').attr("value", obj.packdetail[0].size85);
                    $('#'+lineno+'-size17').attr("value", obj.packdetail[0].size9);
                    $('#'+lineno+'-size18').attr("value", obj.packdetail[0].size95);
                    $('#'+lineno+'-size19').attr("value", obj.packdetail[0].size10);
                    $('#'+lineno+'-size20').attr("value", obj.packdetail[0].size105);
                    $('#'+lineno+'-size21').attr("value", obj.packdetail[0].size11);
                    $('#'+lineno+'-size22').attr("value", obj.packdetail[0].size115);
                    $('#'+lineno+'-size23').attr("value", obj.packdetail[0].size12);
                    $('#'+lineno+'-size24').attr("value", obj.packdetail[0].size125);

            }
            catch(e) {
                console.log('Exception while request..');
            }},
            error: function(){
                console.log('Error while request..');
            }
     });

    });

    //multiply packs

    $("[id^=packs_qty]").on('change', function() {
    var pack_qty = this.value;
    var packname = $(this).attr("data-name");
    console.log("qty:"+pack_qty);
    console.log("packname:"+packname);
    var lineno = this.getAttribute('data-lineno');
    console.log("lineno: "+lineno);
    $.ajax({
        type: "post",
        url: "<?php echo base_url(); ?>products/pack",
        cache: false,
       // ContentType : 'application/json',
        data: {name: packname},
        success: function(json){
        try{
            var obj = jQuery.parseJSON(json);
            console.log(obj);
            console.log("size1: "+obj.packdetail[0].size1*pack_qty);

                $('#'+lineno+'-size1').attr("value", (obj.packdetail[0].size1*pack_qty));
                $('#'+lineno+'-size2').attr("value", obj.packdetail[0].size15*pack_qty);
                $('#'+lineno+'-size3').attr("value", obj.packdetail[0].size2*pack_qty);
                $('#'+lineno+'-size4').attr("value", obj.packdetail[0].size25*pack_qty);
                $('#'+lineno+'-size5').attr("value", obj.packdetail[0].size3*pack_qty);
                $('#'+lineno+'-size6').attr("value", obj.packdetail[0].size35*pack_qty);
                $('#'+lineno+'-size7').attr("value", obj.packdetail[0].size4*pack_qty);
                $('#'+lineno+'-size8').attr("value", obj.packdetail[0].size45*pack_qty);
                $('#'+lineno+'-size9').attr("value", obj.packdetail[0].size5*pack_qty);
                $('#'+lineno+'-size10').attr("value", obj.packdetail[0].size55*pack_qty);
                $('#'+lineno+'-size11').attr("value", obj.packdetail[0].size6*pack_qty);
                $('#'+lineno+'-size12').attr("value", obj.packdetail[0].size65*pack_qty);
                $('#'+lineno+'-size13').attr("value", obj.packdetail[0].size7*pack_qty);
                $('#'+lineno+'-size14').attr("value", obj.packdetail[0].size75*pack_qty);
                $('#'+lineno+'-size15').attr("value", obj.packdetail[0].size8*pack_qty);
                $('#'+lineno+'-size16').attr("value", obj.packdetail[0].size85*pack_qty);
                $('#'+lineno+'-size17').attr("value", obj.packdetail[0].size9*pack_qty);
                $('#'+lineno+'-size18').attr("value", obj.packdetail[0].size95*pack_qty);
                $('#'+lineno+'-size19').attr("value", obj.packdetail[0].size10*pack_qty);
                $('#'+lineno+'-size20').attr("value", obj.packdetail[0].size105*pack_qty);
                $('#'+lineno+'-size21').attr("value", obj.packdetail[0].size11*pack_qty);
                $('#'+lineno+'-size22').attr("value", obj.packdetail[0].size115*pack_qty);
                $('#'+lineno+'-size23').attr("value", obj.packdetail[0].size12*pack_qty);
                $('#'+lineno+'-size24').attr("value", obj.packdetail[0].size125*pack_qty);

        }
        catch(e) {
            console.log('Exception while request..');
        }},
        error: function(){
            console.log('Error while request..');
        }
 });

});

});

1 个答案:

答案 0 :(得分:0)

是否可以将obj.packdetail[0].size1pack_qty转换为数字并尝试, 为此你可以使用

parseInt(obj.packdetail[0].size1) * parseInt(pack_qty);

请试试..