我從數據庫生成值的列表,像這樣:充分利用列表項的值,並將其命名爲jQuery的AJAX調用
<div class="pure-u-1 pure-u-md-2-5 pure-u-lg-2-5 content-left">
<div id="scroll" class="card">
<a href="javascript:showhide('green')"><h2 class="is-center">green</h2></a>
<ul id="greens" class="card-content-ingredients" style="list-style-type: none;">
<?php
foreach ($greens as $green) {
echo "<li>";
echo "<input type='hidden' class='item_id' value='" . $green['id'] . "' name='" . $green['name'] . "'>";
echo "<span class='item-name-small'>" . $green['name'] . "</span>";
echo "<span class='item-description-menu'>" . $green['description'] . "</span>";
echo "<span class='content-right'>";
echo "</span>";
echo "</li>";
}?>
</ul>
</div>
</div>
而且我試圖抓住被點擊的項目一個jQuery功能轉到另一頁:
<script>
$(document).ready(function(){
$('ul.card-content-ingredients li').click(function(){
var form_data = $(this).serialize();
$.ajax({
url: "add-to-cart.php",
type: "POST",
dataType: "json",
data: form_data,
success: function() {}
});
});
});
</script>
而我很難發送實際的數組。我嘗試通過$(this).attr('id")
獲取元素信息,但這並不奏效,我試圖序列化,現在只是抓住吸管。我錯過了什麼?
〔實施例:var_dump($greens)
array (size=31)
0 =>
array (size=10)
'id' => string '19' (length=2)
'name' => string 'Almond Slices' (length=13)
'description' => string 'TOASTED' (length=7)
'price' => string '0.65' (length=4)
'nutritionix_id' => string '529e7dd1f9655f6d35001ce1' (length=24)
'nutritionix_cal' => string '110' (length=3)
'active' => string '1' (length=1)
'ingredienttype_id' => string '2' (length=1)
'subtype' => string '' (length=0)
'in_stock' => string '1' (length=1)
1 =>
array (size=10)
'id' => string '57' (length=2)
'name' => string 'Apples' (length=6)
'description' => string 'Red Jacket' (length=10)
'price' => string '0.65' (length=4)
'nutritionix_id' => string '529e7dd1f9655f6d35001d21' (length=24)
'nutritionix_cal' => string '20' (length=2)
'active' => string '1' (length=1)
'ingredienttype_id' => string '2' (length=1)
'subtype' => string 'fruit' (length=5)
'in_stock' => string '1' (length=1)
2 =>
array (size=10)
'id' => string '58' (length=2)
'name' => string 'Asparagus' (length=9)
'description' => string 'STEAMED' (length=7)
'price' => string '0.65' (length=4)
'nutritionix_id' => string '529e7dd1f9655f6d35001d1e' (length=24)
'nutritionix_cal' => string '0' (length=1)
'active' => string '1' (length=1)
'ingredienttype_id' => string '2' (length=1)
'subtype' => string 'veggies' (length=7)
'in_stock' => string '1' (length=1)
3 =>
array (size=10)
'id' => string '42' (length=2)
'name' => string 'Banana Peppers' (length=14)
'description' => string '' (length=0)
'price' => string '0.65' (length=4)
'nutritionix_id' => string '529e7dd1ea63d49335001ce4' (length=24)
'nutritionix_cal' => string '10' (length=2)
'active' => string '1' (length=1)
'ingredienttype_id' => string '2' (length=1)
'subtype' => string 'veggies' (length=7)
'in_stock' => string '1' (length=1)
4 =>
array (size=10)
'id' => string '43' (length=2)
'name' => string 'Beets' (length=5)
'description' => string 'Fresh' (length=5)
'price' => string '0.65' (length=4)
'nutritionix_id' => string '529e7dd1ea63d49335001cde' (length=24)
'nutritionix_cal' => string '30' (length=2)
'active' => string '1' (length=1)
'ingredienttype_id' => string '2' (length=1)
'subtype' => string 'veggies' (length=7)
'in_stock' => string '1' (length=1)
5 =>
array (size=10)
'id' => string '59' (length=2)
'name' => string 'Black Beans' (length=11)
'description' => string 'Organic' (length=7)
'price' => string '0.65' (length=4)
'nutritionix_id' => string '529e7dd1ea63d49335001d3f' (length=24)
'nutritionix_cal' => string '35' (length=2)
'active' => string '1' (length=1)
'ingredienttype_id' => string '2' (length=1)
'subtype' => string 'veggies' (length=7)
'in_stock' => string '1' (length=1)
那是因爲你不是正確創建輸入數組 – CodeGodie