刷新页面后如何在多个选择框中设置多个选项值

时间:2013-09-10 12:40:18

标签: php javascript ajax

请刷新页面后告诉我如何在多个选择框中设置多个选项值。

我有动态选择框,其ID不同,如下所示

    <select id="select_1" name="select_1"/>
       <option value="a" selected="selected">Data 1</option>
       <option value="b">Data 2</option>
    </select>

    <select id="select_2" name="select_2"/>
       <option value="a">Data 1</option>
       <option value="b" selected="selected">Data 2</option>
    </select>

    <select id="select_3" name="select_3"/>
       <option value="a" selected="selected">Data 1</option>
       <option value="b">Data 2</option>
    </select>

我想在用php和ajax刷新页面后选择所有选择框的所有选项值。

1 个答案:

答案 0 :(得分:0)

我不确定您的数据是如何填充和显示的,但这是一个例子:

<?foreach($selects as $select):?>
<?$selected = ($select->Selected === true) ? 'selected="selected"' :'';?>
<select id="<?=$select->Id?>" name="<?=$select->Name?>"/>
  <?foreach($select->Options as $option):?>
  <option value="<?=$option->Value?>"<?=$selected;?>><?=$option->Text?></option> 
  <?endforeach;?>
<?endforeach;?>

foreach($selects as $select)
{
  $selected = ($select->Selected === true) ? 'selected="selected"' :'';
  echo "<select id=\"{$select->Id}\" name=\"{$select->Name}\"/>\n";
  foreach($select->Options as $option)
  {
    echo "<option value=\"{$option->Value}\"{$selected}>{$option->Text}</option>\n";
  }
  echo "</select>\n";
}