如果数据已经在用户选择的数据库中,我如何选中复选框?对于普通的html,我可以使用checked,但是,我使用javascript这个。有没有办法编辑html而不是javascript?
<td><div align="center"><span class="formlist">
<select id="plant" name="plant[]" class="form-control" multiple="multiple">
<?php
$query_plant = "SELECT * FROM plant WHERE plant_enable=1 ORDER BY plant_name";
$rs_plant = DB_Query($query_plant);
while ($row_plant = DB_FetchRow($rs_plant)) {
$plant.='<option value='.$row_plant["plant_id"].'>' .$row_plant["plant_name"].' ['.$row_plant["plant_id"].']</option>';
}
mysql_free_result($rs_plant);
echo $plant;
?>
</select>
</span></div></td>
<script type="text/javascript">
$(function () {
$('#plant').multiselect({
includeSelectAllOption: true
});
$('#btnSelected').click(function () {
var selected = $("#plant option:selected");
var message = "";
selected.each(function () {
message += $(this).text() + " " + $(this).val() + "\n";
});
alert(message);
});
});
</script>
答案 0 :(得分:1)
您可以创建用户已选择的所有植物的数组。然后像这样做
<td><div align="center"><span class="formlist">
<select id="plant" name="plant[]" class="form-control" multiple="multiple">
<?php
// query to fetch user palnts and crate an array
$userPlants = [1231,1281,1241,1271];
$query_plant = "SELECT * FROM plant WHERE plant_enable=1 ORDER BY plant_name";
$rs_plant = DB_Query($query_plant);
while ($row_plant = DB_FetchRow($rs_plant)) {
$plant.='<option value='.$row_plant["plant_id"];
if (in_array($row_plant["plant_id"], $userPlants)) {
echo " checked ";
}
$plant.= '>' .$row_plant["plant_name"].' ['.$row_plant["plant_id"].']</option>';
}
mysql_free_result($rs_plant);
echo $plant;
?>
</select>
</span></div></td>
答案 1 :(得分:1)
1)首先,您可以将选定的属性添加到用户已选择并存储在数据库中的选项中。像这样
$userPlants = [1,2,3,4]; //fetched value from database . previously user selected .
<option value="<?php echo $row_plant["plant_id"] ?>"
<?php
if(in_array($row_plant["plant_id"], $userPlants))
{
echo " selected";
}
?>
><?php echo $row_plant["plant_name"] ?></option>
$(function () {
$('#plant').multiselect({
includeSelectAllOption: true
});
// $("#plant").multiselect("refresh"); //if you need to refresh the multiselect use like this .
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<link href="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/css/bootstrap.min.css"
rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/js/bootstrap.min.js"></script>
<link href="http://cdn.rawgit.com/davidstutz/bootstrap-multiselect/master/dist/css/bootstrap-multiselect.css"
rel="stylesheet" type="text/css" />
<script src="http://cdn.rawgit.com/davidstutz/bootstrap-multiselect/master/dist/js/bootstrap-multiselect.js"
type="text/javascript"></script>
<td><div align="center"><span class="formlist">
<select id="plant" name="plant[]" class="form-control" multiple="multiple">
<option value='option1'>option1</option>
<option value='option2' selected >option2</option>
<option value='option3' selected >option3</option>
<option value='option4'>option4</option>
<option value='option5'>option5</option>
?>
</select>
</span></div></td>
答案 2 :(得分:0)
<tr>
<td><div align="center">
<select id="plant" name="plant[]" class="form-control" multiple="multiple" >
<?php
$query_po = "SELECT * FROM plant WHERE plant_enable=1 ORDER BY plant_name";
$rs_po = DB_Query($query_po);
while ($row_po = DB_FetchRow($rs_po)) { ?>
<option value="<?php echo $row_po["plant_id"] ?>"
<?php
$user_sql = "SELECT * FROM plant_access WHERE staff_id = '".$STAFF_ID."'";
$user_res = DB_Query($user_sql);
while($user_row = DB_FetchRow($user_res)) {
if($row_po["plant_id"] == $user_row["temp_plant_code"]){
{
echo " selected";
}
} ?>
<?php
}
?>
<?php echo $row_po["plant_name"] ?></option>
<?php
echo $row_po["plant_name"] ;
} ?>
</select>
</div></td>
</tr>
感谢:@jYoThl