简体   繁体   English

如何根据php数组将预选选项设置为多选标记

[英]How to set preselected options into a multiple select tag according to a php array

I have made a multiple select tag and I would like some of its options to be selected according to given PHP array $attr 我已经制作了一个多选标签,我希望根据给定的PHP数组$ attr选择一些选项

<select multiple="multiple" name="isattr[]" id="editAttr">
<?php         
    while($attrresult = mysqli_fetch_array($attrfetch)) {
        echo  "<option>".$attrresult[attr_title]."</option>";
     }
?>
</select>

So if the select tag has the options "Manager" "Programmer" "Teacher" and $attr contains the values "Manager" and "Programmer", these options should be preselected. 因此,如果select标签具有选项“Manager”“Programmer”“Teacher”和$attr包含值“Manager”和“Programmer”,则应预先选择这些选项。

Use in_array() to check if the value is in the $attr array, and apply the selected property to the element based on that : 使用in_array()检查值是否在$attr数组中,并根据该元素将selected属性应用于该元素:

<select multiple = "multiple" name = "isattr[]" id = "editAttr" >
<?php

    $attr = array("Manager", "Programmer");

    while ($attrresult = mysqli_fetch_array($attrfetch)) {

        $selected = in_array( $attrresult[attr_title], $attr ) ? ' selected' : '';

        echo "<option".$selected.">".$attrresult[attr_title]."</option>";

    } 
?> 
< /select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将预选的 PHP 上的前 10 个值设置为多个 select 标签 - How to set preselected the 10 first values on PHP into multiple select tag 如何使用PHP在选择框中选择从多个选项或具有不同值的数组到视图的设置选项 - How to set an option from multiple options or array with different values to views as selected in select box using PHP 如何选择里面的选项 <select multiple=“true”>从PHP数组? - How to select options inside <select multiple=“true”> from php array? 默认情况下如何从数组设置多个选择选项 - How to set multiple select options by default from array 如何设置 <select>标签属性从关联数组php&#39;? - How to 'Set <select> tag attributes From associative array php '? 如何从select中获得价值,在select中有PHP中的预选选项 - How to get value from select in which there is preselected option in PHP 如何为 select 输入动态设置数组选项 - how to set array options dynamically for select input 如何预选多选的选项? (html + php) - How to preselect the options of a multiple select? (html + php) jquery / php从jquery的php数组中选择多个选择的选项 - jquery / php select multiple selected options from php array in jquery 我们如何使用jquery在select标签中设置选项 - How can we set options in select tag using jquery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM