我可能有一个简单的问题。 我有两个选择框,由php动态创建。您可以在此字段中选择数字。我希望在一个选择框中,最小的数字是“预选”,而在另一个框中是最大的。
我的代码:
$res_little=mysqli_query($db_link,"SELECT DISTINCT number FROM ... ORDER BY number ASC");
$res_large=mysqli_query($db_link,"SELECT DISTINCT number FROM ... ORDER BY number ASC");
<select name="little" id="little">
<option></option>
<?php
while($row = mysqli_fetch_array($res_little))
{
echo "<option>";
echo('<p>'.$row['number'].'</p>');
echo "</option>";
}
?>
</select>
<select name="large" id="large">
<option></option>
<?php
while($row = mysqli_fetch_array(res_large))
{
echo "<option>";
echo('<p>'.$row['number'].'</p>');
echo "</option>";
}
?>
</select>
答案 0 :(得分:0)
它应该是这样的:
$res_large=mysqli_query($db_link,"SELECT DISTINCT number FROM ... ORDER BY number ASC");
$max_number=mysqli_query($db_link,"SELECT MAX(number) FROM ... ");
<select name="large" id="large" value='max_number'>
<option></option>
<?php
while($row = mysqli_fetch_array(res_large))
{
echo "<option selected='$max_number'>";
echo('<p>'.$row['number'].'</p>');
echo "</option>";
}
?>
</select>
答案 1 :(得分:0)
您可以简单地使用Jquery ready函数,然后在select下拉列表的第一个选项中添加所选属性。
$("selector option:first").attr('selected','selected');
这将使您的下拉列表的第一个元素被选中。
那么您的代码将如下所示:
<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<?php
$res_little=mysqli_query($db_link,"SELECT DISTINCT number FROM ... ORDER BY number ASC");
$res_large=mysqli_query($db_link,"SELECT DISTINCT number FROM ... ORDER BY number ASC");
?>
<select name="little" id="little">
<?php
while($row = mysqli_fetch_array($res_little))
{
echo "<option value=\"".$row['number']."\">".$row['number']."</option>";
}
?>
</select>
<select name="large" id="large">
<?php
while($row = mysqli_fetch_array($res_large))
{
echo "<option value=\"".$row['number']."\">".$row['number']."</option>";
}
?>
</select>
<script type="text/javascript">
$(document).ready(function(){
$("#little option:first").attr('selected','selected');
$("#large option:first").attr('selected','selected');
});
</script>
答案 2 :(得分:0)
谢谢你这是正确的方法。但我忘了说选择框有一个onchange事件。像这样:
<?php
$res_little=mysqli_query($db_link,"SELECT DISTINCT number FROM ... ORDER BY number ASC");
$res_large=mysqli_query($db_link,"SELECT DISTINCT number FROM ... ORDER BY number ASC");
?>
<select name="little" id="little" ***onchange="from_little(this.value)"***>
<?php
while($row = mysqli_fetch_array($res_little))
{
echo "<option value=\"".$row['number']."\">".$row['number']."</option>";
}
?>
</select>
<select name="large" id="large" ***onchange="to_large(this.value)"***>
<?php
while($row = mysqli_fetch_array($res_large))
{
echo "<option value=\"".$row['number']."\">".$row['number']."</option>";
}
?>
</select>
<script type="text/javascript">
$(document).ready(function(){
$("#little option:first").attr('selected','selected');
$("#large option:first").attr('selected','selected');
});
</script>
所以它应该同时运行这些功能。