简体   繁体   中英

how to display values in dropdown

enter image description here I am trying to display values in a multi-select dropdown, those values are coming from an API. below I have given JSON format in which data is received from API.

the below data is that the user already selected values, now I am displaying a popup to edit the selected values, before this, I need to show the user that the user is previously selected in the multi-select dropdown.

//below is my values coming from API
[
 {id: 1, name: "selected 1"},
 {id: 2, name: "selected 2"}
]

// below is my dropdown element

               <select id="selected"  name="selected[]"  multiple>
                <option  value="" >  </option>
                </select>

If you are using PHP, I think you can do like this.

<select name="selected[]" id="selected" multiple>
  <option selected="selected">Choose one</option>
  <?php
    foreach($users as $user) { 
  ?>
      <option value="<?php echo $user['id'] ?>"><?php echo $user['name'] ?></option>
  <?php
    } 
  ?>
</select> 

Well, as per my understanding from your question, you would like to show all those values which the user selected before. If I'm right, then we may have 2 solutions. 1) To keep the track at server end(ie in the database) 2) To keep track at the client side (ie in cookies on a temporary basis),

Please let me know if I answered your question. If don't then please elaborate your question in more detail with the reference code snippet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM