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条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?