douwo1862 2018-05-09 12:50
浏览 86
已采纳

通过ajax将变量传递给php页面

i've a problem when passwing values with ajax i have a plus and minus buttons and input when i use input it works well to pass the value but with plus or minus it not working it works only on one item and the other items take the same value of the first

 <button class="entry number-minus" data-id="<?=$cartRowID?> " type="button">&nbsp;</button>
 <input type="hidden" name="itemSizesList" value="<?=$itemSizeList?>" />
 <input id="itemid" type="hidden" name="submittedForm" value="<?=$cartItemdID?>_<?=$cartListCounter?>" />
 <input type="hidden" name="itemID" value="<?=encrypt_decrypt("encrypt",$cartItemdID)?>" />
 <input type="hidden" name="cartRowID" value="<?=encrypt_decrypt("encrypt",$cartRowID)?>" />
 <input class="entry number " name="newQty" data-id="<?=$cartRowID?>" value="<?=$cartItemQty?>" style="margin-top: 1px; width:44px"/>
 <button class="entry number-plus" data-id="<?=$cartRowID?>" type="button">&nbsp;</button>
 <input type="submit" class="button style-17" value="Remove" name="delete" />
 <input type="submit" class="button style-15" value="Update cart" name="update" />

ajax code:

// WHEN - IS CLICKED
 $(document).on('click', '.number-minus', function(){
            var rowid = $(this).data("id");
            var qty = $('.number').val();
            // edit_data(eachItemSize, "eachItemSize");

            $.ajax({
                url:"update.php?rowid="+rowid+"qty="+qty,
                method:"POST",
                data:{rowid:rowid , qty:qty},
                dataType:"text",
                success:function(data){
                    alert(data);
                }
            });
        });

        // WHEN + IS CLICKED
        $(document).on('click', '.number-plus', function(){
            var rowid = $(this).data("id");
            var qty = $('.number').val();
            // edit_data(eachItemSize, "eachItemSize");

            $.ajax({
                url:"update.php?rowid="+rowid+"qty="+qty,
                method:"POST",
                data:{rowid:rowid , qty:qty},
                dataType:"text",
                success:function(data){
                    alert(data);
                }
            });
        });
  • 写回答

2条回答 默认 最新

  • douchao1879 2018-05-09 13:13
    关注

    You are grabbing values by class, not by ID!

    Therefore when you call var qty = $('.number').val();, you are probably getting the value of the first item, assuming that this is one row and that there are other rows with these fields too.

    Update: okay, you have multiple forms. So, probably the easiest way to get the correct field of class .number using jQuery is probably using .parent() and .find():

    var qty = $(this).parent().find('.number').val();
    

    This assumes that .parent() will return the form element, and .find() checks only within that form.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Python信息系统tkinter代码错在哪里
  • ¥15 FOR循环语句显示查询超过300S错误怎么办
  • ¥15 个人通讯录管理系统 C语言 程序设计 需要能运行成功的 结构体和数组 Visual C++编译器
  • ¥15 数电设计题 没有设计思路 不知道用什么芯片进行设计 求提供设计思路
  • ¥15 在动态多目标优化问题中,第一幅图展示的是问题DF6的相关定义和绘制的POS和POF图,请问图中公式PS(t)和PF(t)是如何推导的
  • ¥60 先数学建模,接着设计一种优化算法结合案例给出智能仓储四向穿梭车的调度计划
  • ¥15 Errno2:No such file or directory,在当前文件确实没有该图片,怎么解决?
  • ¥15 博世摄像头数据存储的问题(iscsi)
  • ¥15 如何实现对学生籍贯信息管理系统的选择排序
  • ¥15 写一个51单片机的时钟代码