php - 自动从下拉列表中获取第一项到输入

标签 php jquery html mysql input

是否可以在页面加载时自动从下拉列表中获取第一项到输入?例如,下拉列表有 2 个选项:happysad。当页面加载时,我希望 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/

相关文章:

php - echo 随机颜色不起作用

javascript - 用于删除灯箱联系表单滚动条的CSS

php - laravel ColumnDefinition 类中的方法在哪里实现?

php - 自动将尾部斜杠添加到 urlencoded url

jquery - 防止 HTML5 contenteditable 字段中 'return' 键的默认行为

javascript - 使用 .text 的 JS 函数没有完全按照预期替换文本

html - 为什么在某些情况下背景颜色会被忽略?

php - mysql PDO ON DELETE CASCADE - 不工作

javascript - 动态地将值合并到对象属性 - 首先是未定义的

javascript - 以编程方式关注移动 safari 中的下一个输入字段