我有这个 foreach 循环:
foreach ($userItems_get as $item => $value) {
if ($value['prefab'] == 'wearable') {
echo $value['name'] . "</br>";
echo "<img src=\"{$value['image_inventory']}.png\" width=\"90\" height=\"60\">" . "</br>";
if (!isset($value['item_rarity'])) {
$rarity = "common";
} else {
$rarity = $value['item_rarity'];
}
echo $rarity . "</br>";
foreach ($userItemsLoad as $key => $values) {
if ($item == $values['defindex']) {
echo $values['id'] . "</br></br>";
break;
}
}
}
}
以这种格式输出数据: http://puu.sh/kVTjk/c1471e903a.jpg
我希望用户选择他想要交易/使用的元素,并且我想接收该元素的 ID,即底部的整数值,用户应该能够选择多个元素?
我该如何实现这个目标?最好的方法是什么?谢谢。
最佳答案
有很多方法可以实现这一目标。
您可以使用普通提交:
<form method="POST" action="script.php">
<table>
<tr>
<th></th>
<th>Image</th>
<th>Name</th>
</tr>
<?php
foreach ($userItems_get as $item => $value) {
if ($value['prefab'] == 'wearable') {
$id = "";
foreach ($userItemsLoad as $key => $values) {
if ($item == $values['defindex']) {
$id = $values['id'];
break;
}
}
?>
<tr>
<td><input type="checkbox" name="itemSelect[]" class="itemSelect" value="<?php echo $id; ?>" /></td>
<td>
<img src="<?php echo $value['image_inventory']; ?>.png" width="90" height="60">
</td>
<td>
<?php echo $value['name']; ?><br />
<?php
if (!isset($value['item_rarity'])) {
$rarity = "common";
} else {
$rarity = $value['item_rarity'];
}
?>
</td>
</tr>
<?php
}
}
?>
</table>
<button type="Submit">
Normal Submit
</button>
<button type="button" id="ajSubmit">
Ajax Submit
</button>
</form>
脚本.php:
<?php
echo "<pre>";
print_r($_POST['itemSelect']);
echo "</pre>";
?>
或者使用 jQuery:
<script>
$(function() {
$("#ajSubmit").click(function() {
var selectedItemIds = $("input.itemSelect").map(function(){
return $(this).val();
}).get();
});
});
</script>
selectedItemIds
将保存所有 ID 值。
关于javascript - 从 foreach 输出中单独选择多个选择的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33320171/