2016-12-12 65 views
0

這是我的表如何在JavaScript中獲取foreach輸入值並將其傳遞給ajax數據?

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

這裏是我的javascript代碼

<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) { 
       } 
      } 
     }); 
    }); 
}); 

在我使用的foreach形式。如果我有2個數據,所以它會顯示2個輸入標籤,我只是不知道如何保存該2個輸入的值並將其傳遞到data: {qty,price,pro_id,customer_id},

先謝謝您!

回答

2

提供一個共同的類到使用循環等產生的所有文本框:

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

Jquery的:此數組即ARR在數據AJAX調用{}部使用

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

通序列化或索引。

+0

謝謝它的工作。@ Mayank – Vky

+0

這是偉大的。接受答案,如果它適合你:) –

+0

是的隊友,但你知道我們不能接受即時。有時間限制 – Vky

1

不要使用:

data: { qty, price, pro_id, customer_id }, 

而是使用:

data: $("#BargainForm").serialize(), 

這將只工作,如果你的$attribute['store_label']名稱是唯一的。

1

更換

data: {qty,price,pro_id,customer_id}, 

通過

data: "qty="+qty+"&price="+price+"&pro_id="+pro_id+"&customer_id="+customer_id+"&att_lable="+att_lable, 
相關問題