【问题标题】:Best way to select multiple selections seperatly from foreach output从 foreach 输出中分别选择多个选项的最佳方法
【发布时间】:2015-10-24 15:57:11
【问题描述】:

我有这个 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,它是底部的整数值,用户应该能够选择多个项目吗?

我如何做到这一点?最好的方法是什么?谢谢。

【问题讨论】:

  • 首先为您的 html 添加一些结构。在元素属性中存储需要的内容

标签: javascript php html select foreach


【解决方案1】:

有很多方法可以实现这一点。

你可以使用普通的提交:

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

Script.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 值。

【讨论】:

  • 我对 jquery 不是很熟悉,但我希望能够做这样的事情:puu.sh/kVXo0/c1f885af24.jpg 在这里您只需单击项目并单击提交按钮?我怎么做这样的桌子?谢谢。
  • 我假设你有样式表和 html。
  • 我不知道,我会试着做我猜.. 在 html 和 css 方面没有很多经验!但我会尝试回复。
  • 好的,我这里有 html,这是新循环?你现在能帮我吗?谢谢。 pastebin.com/jPGE8Y7W
【解决方案2】:

为每个项目添加一个复选框,并为复选框添加一个类以供参考

<input class="itemId" type="checkbox" value="$values['defindex']">

您可以使用JQuery 选择所有选中的复选框,然后在每个循环中循环

//Select all input elements which has class of itemId and it is checked then loop through with each.
$("input.itemId[checked='checked']").each(function(){
    var itemId = $(this).val();
    alert(itemId);

    //Do something else

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-22
    • 2022-07-16
    • 1970-01-01
    • 2022-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多