发布于 2024-08-18 14:30:33 字数 138 浏览 2 评论 0原文

如何将 selected="selected" 位添加到来自发送的 $_POST 数据的 HTML

How can i add the selected="selected" bit to the option in an HTML <select> input from the sent $_POST data without an if statement within each option?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绳情 2024-08-25 14:30:33
<?php

$options = array(1 => 'Banana', 2 => 'Apple');

foreach ($options as $key => $value) {
    echo '<option value=""';
    if ($key == $_POST['fruit']) echo ' selected="selected"';
    echo '>'.$value.'</option>';
}

?>
<?php

$options = array(1 => 'Banana', 2 => 'Apple');

foreach ($options as $key => $value) {
    echo '<option value=""';
    if ($key == $_POST['fruit']) echo ' selected="selected"';
    echo '>'.$value.'</option>';
}

?>
停滞 2024-08-25 14:30:33

从编程上来说,你可以这样做:

$optionNames = array('This', 'Is', 'A', 'Test');
echo '<select id="testselect" name="testselect">';
foreach($optionNames as $currentOption) {
    echo '<option value="'.$currentOption.'"';
    echo $_POST['testselect'] == $currentOption ? ' selected="selected"' : '';
    echo '>'.$currentOption.'</option>';
}
echo '</select>';

必须承认我目前没有开发盒来测试上面的代码,但应该没问题。 (如果没有,抱歉。):-)

Programatically, you could do it like this:

$optionNames = array('This', 'Is', 'A', 'Test');
echo '<select id="testselect" name="testselect">';
foreach($optionNames as $currentOption) {
    echo '<option value="'.$currentOption.'"';
    echo $_POST['testselect'] == $currentOption ? ' selected="selected"' : '';
    echo '>'.$currentOption.'</option>';
}
echo '</select>';

Must confess I don't have a dev box up at the moment to test the above code, but it should be OK. (Apologies if not.) :-)

把人绕傻吧 2024-08-25 14:30:33

我认为对每个选项使用 if 语句是最有效的。

但是您可以创建一个包含除要选择的选项位置之外的空字符串的数组,以消除 if 语句。

$options = array(1 => 'Banana', 2 => 'Apple', 3 => 'Orange');
$selected_options = array_fill(1, sizeof($options), "");
if(array_key_exists($_POST['fruit'], $options))
    $selected_options[$_POST['fruit']] = " selected=\"selected\"";

echo '<select id="fruit" name="fruit">';
foreach($options as $optionId => $optionText)
    echo '<option value="'.$optionId.'"'.$selected_options[$optionId].'>'.$optionText.'</option>';
echo '</select>';

I suppose using an if statement for each option is most efficient.

But you can create an array containing empty strings except for the location of the option you want to select in order to eliminate the if statement.

$options = array(1 => 'Banana', 2 => 'Apple', 3 => 'Orange');
$selected_options = array_fill(1, sizeof($options), "");
if(array_key_exists($_POST['fruit'], $options))
    $selected_options[$_POST['fruit']] = " selected=\"selected\"";

echo '<select id="fruit" name="fruit">';
foreach($options as $optionId => $optionText)
    echo '<option value="'.$optionId.'"'.$selected_options[$optionId].'>'.$optionText.'</option>';
echo '</select>';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文