在PHP / HTML的列表框中恢复多选选择

时间:2014-02-26 16:03:07

标签: php html multi-select

所以我有一个多选列表框......

<select id="extfeat" name="extfeat[]" size="6" multiple="">  
    <option value="Wheel_Chair Accessible">Wheel Chair Accessible</option>  
    <option value="Fruit_Trees">Fruit Trees</option>   
    <option value="Large_Trees">Large Trees</option>   
    <option value="Rolling">Rolling</option>   
    <option value="Other">Other</option>   
    <option value="Level">Level</option>   
    <option value="Wooded">Wooded</option>   
    <option value="Outbuildings">Outbuildings</option>   
    <option value="Gazebo">Gazebo</option>   
    <option value="Workshop Area">Workshop Area</option>   
    <option value="Sauna">Sauna</option>   
    <option value="Courtyard">Courtyard</option>   
    <option value="Lake_Access">Lake Access</option>   
    <option value="Tillable">Tillable</option>   
    <option value="Deck">Deck</option>   
    <option value="Lakeview">Lakeview</option>   
    <option value="Propane_Tank_-_Leased">Propane Tank - Leased</option>   
    <option value="Dock">Dock</option>   
    <option value="Landscaped">Landscaped</option>  
    <option value="Propane_Tank - Owned">Propane Tank - Owned</option>   
    <option value="Fenced_Yard">Fenced Yard</option>   
    <option value="Water_Frontage">Water Frontage</option>   
    <option value="Concrete_Parking">Concrete Parking</option>   
    <option value="Pond">Pond</option>   
    <option value="Tennis_Court">Tennis Court</option>   
    <option value="Circle_Drive">Circle Drive</option>   
    <option value="Horses_Allowed">Horses Allowed</option>   
    <option value="Patio_Enclosed">Patio Enclosed</option>   
    <option value="Sprinkler_System">Sprinkler System</option>   
    <option value="Brick_Trim">Brick Trim</option>   
    <option value="Guest_House">Guest House</option>   
    <option value="Terraced">Terraced</option>   
    <option value="Hot_Tub">Hot Tub</option>   
    <option value="Outdoor_Sign">Outdoor Sign</option>   
    <option value="Garden">Garden</option>   
    <option value="Above_ground_pool">Above ground pool</option>   
    <option value="Ground_Lvl_Access">Ground Lvl Access</option>   
    <option value="Handicap_Access">Handicap Access</option>   
    <option value="Patio">Patio</option>  
    <option value="Garage_Opener">Garage Opener</option>   
    <option value="In-ground_Pool">In-ground Pool</option>   
    <option value="Greenhouse">Greenhouse</option>   
    <option value="Pasture">Pasture</option>   
    <option value="Shed">Shed</option>   
    <option value="Boat_House">Boat House</option>   
    <option value="Antenna">Antenna</option>   
    <option value="Generator">Generator</option>   
    <option value="Asphalt_Parking">Asphalt Parking</option>   
    <option value="Sloping">Sloping</option>   
    <option value="Porch">Porch</option>   
    <option value="Satellite_Dish">Satellite Dish</option>   
    <option value="Screened_Porch">Screened Porch</option>   
    <option value="Underground_Pet_Fence">Underground Pet Fence</option>   
    <option value="Pool">Pool</option> 

  </select>          

所以假设用户选择7个选项并进行搜索...我的搜索有一个精简选项,这个以及许多其他字段被保存并存储在隐藏的div中,可以通过按下按钮再次显示。 ..

问题是,在POST之后,extfeats []变量没有保留选择...我需要以某种方式将现有的多项选择作为默认值进行传输。

2 个答案:

答案 0 :(得分:1)

如果您知道选择了哪些选项,则默认情况下可以将其标记为选中:selected="selected"

<select id="extfeat" name="extfeat[]" size="6" multiple="">  
  <option value="Wheel_Chair Accessible">Wheel Chair Accessible</option>  
  <option value="Fruit_Trees" selected="selected">Fruit Trees</option>
  // ..
</select>

在备注上multiple=""应该是multiple="multiple"

答案 1 :(得分:0)

好吧......事实证明它比我想象的更容易......

<select id="extfeat" name="extfeat[]" size="6" multiple="">  
  <option value="Wheel_Chair Accessible" <? if (in_array("Wheel_Chair Accessible",$extfeat)) echo "selected" ?> >Wheel Chair Accessible</option>  
  <option value="Fruit_Trees" <? if (in_array("Fruit_Trees",$extfeat)) echo "selected" ?>>Fruit Trees</option>  
  ... 

向Frits van Campen致敬,因为你向正确的方向轻轻推动;)