douhui9380 2014-06-13 16:34
浏览 282
已采纳

使用jquery从AJAX循环遍历多维数组以填充select

A multidimensional returned from php, here is my php array:

$brands = array("Bmw" => "200" ,"Mercedes" => "201", "Audi" => "202");

When the data is returnend how can I populate a select with this info?

Lets assume that I need fill a select with Brands and their ID.

<select>
  <option value="200">Bmw</option>
  <option value="201">Mercedes</option>
  <option value="202">Audi</option>
</select>

I dont know how to loop through the returned array to fill the select.

Thanks a lot!!

  • 写回答

1条回答 默认 最新

  • duanmo6937 2014-06-13 16:42
    关注

    Use it this way.

    $brands = array("Bmw" => "200" ,"Mercedes" => "201", "Audi" => "202");
    $output = "<select>";
    foreach($brands as $keys => $val){
        $output .= '<option value="'.$val.'">'.$keys.'</option>';
    }
    $output .= "</select>";
    echo $output;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?