控制选择表单上的选项数量
control number of options on select form
我有一个int类型的php变量$a
。现在如果$a=1;
,那么前两个选项应该只可见,如果$a=2;
,那么前三个选项应该只出现,以此类推。我怎样才能做到这一点呢?
echo "<form class='form-horizontal'>
<fieldset >
<span class='control-group' >
<span class='controls'>
<select id='fl' class='form-control' style='cursor:pointer;'>
<option " . ($default == 0 ? "selected='selected'" : "") . " style='display:none;' value='0'>Select</option>
<option " . ($default == 1 ? "selected='selected'" : "") . " value='1'>Option1</option>
<option " . ($default == 2 ? "selected='selected'" : "") . " value='2'>Option2</option>
</select>
</span>
</span>
<div><button id='mybtn' type='button'>Save</button></div>
</fieldset>
</form>";
似乎是for
循环的绝佳机会。
// put your values into an array for easy access inside the loop
$options = array(
1 => "Option1",
2 => "Option2",
3 => "Option3",
etc...
);
// output the beginning of the <select> html
echo "<select id='fl' class='form-control' style='cursor:pointer;'>
<option " . ($default == 0 ? "selected='selected'" : "") . " style='display:none;' value='0'>Select</option>";
// loop through items until we reach our limit, set in $a
for ($i = 1; $i < $a; $i++) {
echo "<option " . ($default == $i ? "selected='selected' " : "") . "value='" . $i . "'>" . $options[i] . "</option>";
}
// output the end of the <select> html to close it off
echo "</select>"
根据$a
的值在echo
之前放置选项,并在echo
中使用该变量
<?php
$options = '';
if($a==1)
{
$options = "<option " . ($default == 0 ? "selected='selected'" : "") . " style='display:none;' value='0'>Select</option>
<option " . ($default == 1 ? "selected='selected'" : "") . " value='1'>Option1</option>";
}
else if($a==2)
{
$options = "<option " . ($default == 0 ? "selected='selected'" : "") . " style='display:none;' value='0'>Select</option>
<option " . ($default == 1 ? "selected='selected'" : "") . " value='1'>Option1</option>
<option " . ($default == 2 ? "selected='selected'" : "") . " value='2'>Option2</option>";
}
echo "<form class='form-horizontal'>
<fieldset >
<span class='control-group' >
<span class='controls'>
<select id='fl' class='form-control' style='cursor:pointer;'>
".$options."
</select>
</span>
</span>
<div><button id='mybtn' type='button'>Save</button></div>
</fieldset>
</form>";
?>
相关文章:
- 使用此选项选择父类内部的类
- 更新:仅根据单选按钮和所选选项选择特定项目
- 使用PHP通过HTML表单选项选择器过滤MYSQL结果
- ng-单击在IE中不起作用的选项选择Angularjs
- 从选项选择中删除值
- On为<选项>选择等效项
- 用户界面先前选择的选项选择更改
- 使用ng重复或ng选项和删除空白选项选择长方体角度
- 在HTML5、CSS、JavaScript中添加选项选择器,类似于移动应用程序中的选项设置
- 输入:选中,选项:选择选择器
- 根据使用angularjs从下拉菜单中选择的选项选择适当的复选框
- 如何根据 html 中的选项/选择重定向用户(单击按钮后)
- 使用ng选项选择中的ng选项进行过滤器下拉列表
- 如何设置选项选择的值
- 如何搜索长下拉选项/选择列表
- jQuery - 根据下拉选项选择列出结果
- 从选项选择单击时打开 2 个 URL
- 在 Angular JS 中为 ng 选项选择默认值
- 下拉选项选择的操作
- 通过单击单选按钮清除所有选项选择条目