是否可以在页面加载时自动从下拉列表中获取第一项到输入?例如,下拉列表有 2 个选项:happy
和 sad
。当页面加载时,我希望 happy
出现在页面加载的输入中。到目前为止,我得到一个空的输入,用户需要在下拉列表中选择选项。
J查询代码:
<script type="text/javascript">
$(document).ready(function() {
$("#username_select").change(function() {
$("#username_custom").val($(this).val());
});
});
</script>
用户名_自定义 ID:
<input id="username_custom" name="custom" value="
<?php
if(isset($_POST['getusername'])) {
$getusername = $_POST['getusername'];
echo "".$getusername."";
}
if(empty($_POST['username'])) {
echo "".$row['username']."";
}
?>
">
用户名_选择id:
<select id="username_select" name="getusername">
<?php
$username = $_SESSION['username'];
$result = mysql_query("SELECT username FROM account WHERE websiteusername='".$username."'");
while($row = mysql_fetch_array($result)) {
echo "<option value='".$row['username']."'>".$row['username']."</option>";
}
?>
</select>
最佳答案
在input中显示select的选中值
$("#YourInputId").val($("#username_custom").val());
设置select的第一个选项文本
$("#YourInputId").val($("#username_custom option:eq(0)").text());
更改选择时更改文本框的值
$(document).ready(function() {
$("#username_select").change(function() {
$("#username_custom").val($(this).val());
$("#YourInputId").val($("#username_custom").val());
});
});
关于php - 自动从下拉列表中获取第一项到输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15675049/