我在将元素数组传递给 Ajax 请求时遇到问题。应该怎么做?
var category = $('select[name="category[]"]').serialize();
$.post(host + '/form/ajax.php', {
category:category
},
function(data) {
console.log(data);
});
在 PHP 中,我需要知道有多少类别元素,例如:
<?php echo count($_POST['category']) ?>
// should return 3
HTML
<select name="category[]">
<option value="1">CPU</option>
<option selected value="2">Hard Drive</option>
</select>
<select name="category[]">
<option value="1">CPU</option>
<option selected value="2">Hard Drive</option>
</select>
<select name="category[]">
<option selected value="1">CPU</option>
<option value="2">Hard Drive</option>
</select>