简体   繁体   English

从下拉框中获取选定的值,并用于填充文本框

[英]Get selected value from drop down box and use to populate text box

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. 我有一个Web表单,其中包含一个下拉框,该框由对使用php的MySQL服务器的请求填充以及一个文本框。 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. 我想要的是用户在下拉框中单击一个值,该值用于填充Web表单中的文本框。 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. 我假设它需要实现onClick / onSelect侦听器,但是我不确定如何做到这一点。 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>

Theres no such thing as echo in javascript. 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() 如果您只想查看#dropDownBox选项的值:selected包含,请使用console.log()并检查您的控制台(f12而不是console选项卡)或使用alert()

now to populate the textboxdiv i think you would want just to put it in something like this: 现在填充textboxdiv,我想你只想把它放在这样的地方:

<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: 另外,如果您使用jquery,我认为您应该代替onUpdate做这样的事情:

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

See javascript's change event: change 查看javascript的change事件: change

and here is the fiddle: http://jsfiddle.net/michasko/k4hYE/ 这是小提琴: http : //jsfiddle.net/michasko/k4hYE/

select.onchange = function() {
    input.value = this.value;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM