簡體   English   中英

帶有用於ajax的jquery的序列化參數

[英]serialized parameters with jquery for ajax

我需要幫助來准備$ .ajax提交的參數。

我有多對html輸入(i對):

> <input type="hidden" value="31" name="product_id"> <input
> type="hidden" value="3" name="quantity">
> 
> <input type="hidden" value="34" name="product_id"> <input
> type="hidden" value="1" name="quantity">

如果我使用以下jQuery代碼:

   function send(){      

   var parameter1 =$("input[name$='product_id']").serialize();
   var parameter2 = $("input[name$='quantity']").serialize();

  $.ajax({
   type: "POST",
   url: 'index.php?route=checkout/cart/add', 
   data: parameter1+'&'+parameter2  
  });
};

我發送這樣的參數:

product_id = 31&product_id = 30&quantity = 2&quantity = 2

如何傳遞這樣的參數:

product_id = 31&quantity = 2&product_id = 30&quantity = 2

謝謝

如果您想以相同的順序解析它們,它們在html中

data = '';
$('input[type="hidden"]').each(function(){
    data += '&' + $(this).attr('name') + '=' + $(this).attr('value');'
});

進行以下更改

在html中

<form id="frm">
<input type="hidden" value="31" name="product_id[]">
<input type="hidden" value="3" name="quantity[]">
<input type="hidden" value="34" name="product_id[]">
<input type="hidden" value="1" name="quantity[]">
</form>

在js中

function send(){      

$.ajax({
 type: "POST",
 url: 'index.php?route=checkout/cart/add', 
  data: jQuery('#frm').serialize()  
 });
};

您將獲得全球$ _POST的價值

嘗試這個:

function send(){      
    var data = {
        parameter1:$("input[name$='product_id']").val(),
        parameter2:$("input[name$='quantity']").val()
    };



  $.ajax({
   type: "POST",
   url: 'index.php?route=checkout/cart/add', 
   data: JSON.stringify(data),
   success : function(msg){
   // your code here       
   },
   error : function(msg){
   // your code here
   },  
  });
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM