问题
I have an html select that is populated with DATA from a query using a foreach loop. It looks something like this
$client = $wpdb->get_results("SELECT string FROM `file` WHERE 
`code` = 002 AND `status` = 2");
echo 'Filter by client: ';
  echo '<select name="client_list">';
  foreach ($client as $key => $row) {
    $value = $row->string;
    echo 
  '<option value='.$value.'>'
  .$value. '</option>';
  }
  $client = $_GET['client_list'];
  echo '</select>';
It serves as a filter to display data based on the selected option value. The table that it filters looks somehting like this
   |client  | file              | 
   |------  |-------------------|
   |client1 | file00000         |
   |client2 | file00002         |
Now I want to make the first option value (the default value) of the html empty. How do I do this?
回答1:
you can use this
      $client = $wpdb->get_results("SELECT string FROM `file` WHERE 
     `code` = 002 AND `status` = 2");
    echo 'Filter by client: ';
    echo '<select name="client_list"><option value=""></option>';
    foreach ($client as $key => $row) {
        $value = $row->string;
        echo 
       '<option value='.$value.'>'
        .$value. '</option>';
    }
    $client = $_GET['client_list'];
    echo '</select>';
回答2:
Add an empty option first:
echo '<select name="client_list"><option value=""></option>';
on submit if client_list is empty you know someone didn't choose anything.
来源:https://stackoverflow.com/questions/46582289/leave-default-option-value-empty-in-html-select