如何在 Bootstrap 中预填充 Select 列表的值?
How do I prepopulate the value of Select List in Bootstrap?
我正在使用 bootstrap 框架创建一个简单的表单,用于编辑 SQL 数据库记录。简而言之,我只发布了一小段代码,一个 Select 列表输入而已。如何自动设置列表中的特定值?我希望我可以使用
<select class="form-control" id="..... value="Option1">
但这没有用。我的最终结果是使用 SQL 查询设置值。
<!--- Location Drop Down--->
<form class="form-horizontal" role="form" method="post" action="post_record.php">
<div class="form-group">
<label for="inputLocation" class="col-sm-2 control-label">Location</label>
<div class="col-sm-4">
<select class="form-control" id="inputLocation" name="inputLocation" value="Option1">
<option value="0">Please Select</option>
<option value="1">Option1</option>
<?php
$servername = "localhost";
$username = "*********";
$password = "*********";
$dbname = "**********";
// Create Connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
//Check connection
if (!$conn) {
trigger_error("Connection failed: " . mysqli_connect_error());
}
if ($conn) {
echo "success";
}
$stmt = "SELECT DISTINCT `ACT_LOCATION` FROM `actual_data`";
$result = mysqli_query($conn,$stmt) or die(mysqli_error());
while(list($category) = mysqli_fetch_row($result)){
echo '<option value="'.$category.'">'.$category.'</option>';
}
?>
</select>
</div>
</div>
为要默认选择的 <option>
添加属性 selected="selected"
:
<option value="1" selected="selected">Option1</option>
我正在使用 bootstrap 框架创建一个简单的表单,用于编辑 SQL 数据库记录。简而言之,我只发布了一小段代码,一个 Select 列表输入而已。如何自动设置列表中的特定值?我希望我可以使用
<select class="form-control" id="..... value="Option1">
但这没有用。我的最终结果是使用 SQL 查询设置值。
<!--- Location Drop Down--->
<form class="form-horizontal" role="form" method="post" action="post_record.php">
<div class="form-group">
<label for="inputLocation" class="col-sm-2 control-label">Location</label>
<div class="col-sm-4">
<select class="form-control" id="inputLocation" name="inputLocation" value="Option1">
<option value="0">Please Select</option>
<option value="1">Option1</option>
<?php
$servername = "localhost";
$username = "*********";
$password = "*********";
$dbname = "**********";
// Create Connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
//Check connection
if (!$conn) {
trigger_error("Connection failed: " . mysqli_connect_error());
}
if ($conn) {
echo "success";
}
$stmt = "SELECT DISTINCT `ACT_LOCATION` FROM `actual_data`";
$result = mysqli_query($conn,$stmt) or die(mysqli_error());
while(list($category) = mysqli_fetch_row($result)){
echo '<option value="'.$category.'">'.$category.'</option>';
}
?>
</select>
</div>
</div>
为要默认选择的 <option>
添加属性 selected="selected"
:
<option value="1" selected="selected">Option1</option>