dougengqiu8031 2014-07-28 09:57 采纳率: 100%
浏览 127

从下拉框中获取选定的值,然后使用填充文本框

I have a web form that contains a drop down box that is populated by a request to a MySQL server with php, and a text box. The value in the text box is currently passed off to another page for processing. This all works fine but as there are a lot of submissions with the same value being passed through I've decided to implement the drop down box within the form to allow a user to select from the options that have been submitted previously.

What I would like is when the user clicks on a value in the drop down box that the value is used to populate the text box in the webform. I'm having trouble pulling this value out. I'm assuming that it requires an onClick/onSelect listener to be implemented but I'm not entirely sure how to do this. Any help would be greatly appreciated!

edit for code, currently I'm just trying to get the value to echo to the page as a proof of concept, but it will eventually need to go into the text box.

 <script type="text/javascript">
function moveToTextBox()
    echo $("#dropDownBox option:selected").text(); 
</script>

<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

        Enter Asset Type<br/>
        <select name="currentItems" onUpdate="moveToTextBox()" id="dropDownBox">
        <?php  
            $pdo = Database::connect();
            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $sql = "SELECT item FROM all_Items";
            $result=$pdo->query($sql);
            $array=null;
            while($row=$result->fetch(PDO::FETCH_ASSOC))
            {
                echo "<option value=\"item1\">" . $row['item'] . "</option>";
            }
            Database::disconnect(); 
        ?>
        </select><br/>
        <input type="text" name="newItem" id="textId"><span class="error"> <?php echo $newItemError;?></span>
    <br/>
    <input type="submit" name="submit" value="Submit"> 
</form>
  • 写回答

2条回答 默认 最新

  • doujiaoang69440 2014-07-28 10:30
    关注

    Theres no such thing as echo in javascript.

    If you just want to see what value the #dropDownBox option:selected contains use console.log() and check your console (f12 than console tab) or use alert()

    now to populate the textboxdiv i think you would want just to put it in something like this:

    <script type="text/javascript">
       function moveToTextBox(){
         content = $("#dropDownBox option:selected").text();
         $('#randomTextPopulateDivYouNeedToMakeSomewhere').html(content);
       }
    </script>
    

    Also if youre using jquery i think you should instead of onUpdate do something like this:

       $("#dropDownBox").change( function() {
         content = $("#dropDownBox option:selected").text();
         $('#randomTextPopulateDivYouNeedToMakeSomewhere').html(content);
       });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)