double820122 2016-12-12 07:13
浏览 124
已采纳

如何在javascript中获取foreach输入值并将其传递给ajax数据?

This is my Form

<form method="post" id="BargainForm">
    <input type="hidden" name="pro_id" class="pro_id" id="pro_id" value="<?php echo $pro_id; ?>">
    <input type="hidden" name="customer_id" class="customer_id" id="customer_id" value="<?php echo $customer_id; ?>">
    <input type="text" name="bargain_qty" class="bargain_qty" id="bargain_qty" placeholder="Qty" required/></br></br>
    <?php if($productType = 'Configurable'): ?>
        <?php foreach($attributes as $attribute): ?>
            <input type="text" name="<?php echo strtolower($attribute['store_label']); ?>" 
                                        class="<?php echo strtolower($attribute['store_label']); ?>" 
                                        id="<?php echo strtolower($attribute['store_label']); ?>" 
                                        placeholder="<?php echo $attribute['store_label'] ?>"></br></br>
        <?php endforeach; ?>
    <?php else: ?>

    <?php endif; ?>
    <input type="text" name="bargain_price" class="bargain_price" id="bargain_price" placeholder="Total Price" required/></br></br>
    <input type="submit" name="bargain_btn" class="bargain_btn">
</form>

Here is my javascript code

<script type="text/javascript">
$(function () {
    $('.bargain_btn').click(function(e) {
        var qty = $( "#bargain_qty" ).val();
        var price = $( "#bargain_price" ).val();
        var pro_id = $( "#pro_id" ).val();
        var customer_id = $( "#customer_id" ).val();
        var att_lable = $( "#<?php echo strtolower($attribute['store_label']); ?>" ).val();
        alert(att_lable);
        e.preventDefault();
        $.ajax({
            url: "<?php echo Mage::getUrl('bargain/Ajax/index'); ?>",
            type: "POST",
            data: {qty,price,pro_id,customer_id},
            success: function(data) {
                if(data.success) {
                }
            }
        });
    });
});

In form i am using foreach. If i have 2 data so it will display 2 input tags, i just don't know how to save values of that 2 input and pass it to data: {qty,price,pro_id,customer_id},

Thank you in advance!

  • 写回答

3条回答 默认 最新

  • douquanhui5735 2016-12-12 07:18
    关注

    Provide a common class to all textbox that are generated using loop like:

    foreach(...)
    {
       echo '<input class="myTxt" value="" id="" />';
    }
    

    Jquery:

    var arr = [];
    $('.myTxt').each(function(){
        arr.push($(this).val());
    });
    

    pass this array i.e. arr to ajax call in data{} section using serialize or on an index.

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

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条