简体   繁体   English

根据下拉值自动填充文本框

[英]Auto fill text box depending on Drop Down value

This might be a stupid question but I would like to have a clarification on how to go about this.这可能是一个愚蠢的问题,但我想澄清一下如何解决这个问题。 I have come across quite a few articles that let to set the values of a text box depending on the choices made in a drop down menu using jQuery and Ajax.我遇到过很多文章,它们允许根据使用 jQuery 和 Ajax 在下拉菜单中所做的选择来设置文本框的值。 My issue is when I'm trying to do the same depending on the choices made in 5 drop down menus.我的问题是当我根据 5 个下拉菜单中的选择尝试做同样的事情时。 I have the Id values stored in the database which should be used to populate the text box.我将 Id 值存储在数据库中,该值应该用于填充文本框。 Can anyone guide on how to go about this issue with multiple drop downs.任何人都可以通过多个下拉菜单指导如何解决这个问题。 This is my code so far:到目前为止,这是我的代码:

    <?php
     $sql1="SELECT Schlungen  FROM schulung as s"; 
     $result=mysql_query($sql1); 
     echo "<p align='left'> <strong>Schulung 1</strong> <select name='Schlungen1'>           <option value default></option>";
     while ($row = mysql_fetch_array($result)) 
     {
     echo "<option value='" . $row['Schlungen'] . "'>" . $row['Schlungen'] . "   </option>";
      }
      echo "</select>";
      ?>

      <?php
      error_reporting(0);
      //Drop Down for Schulung 2
      $sql2="SELECT Schlungen  FROM schulung as s"; 
      $result=mysql_query($sql2); 
      echo "<p align='left'> <strong>Schulung 2</strong> <select name='Schlungen2'>  <option value default></option>";
      while ($row = mysql_fetch_array($result)) 
      {
      echo "<option value='" . $row['Schlungen'] . "'>" . $row['Schlungen'] . " </option>";
       }
      echo "</select>";
      ?>


      <?php
      error_reporting(0);
      //Drop Down for Schulung 3 
      $sql3="SELECT Schlungen  FROM schulung as s"; 
      $result=mysql_query($sql3); 
      echo "<p align='left'> <strong>Schulung 3</strong> <select name='Schlungen3'> <option value default></option>";
      while ($row = mysql_fetch_array($result)) 
      {
      echo "<option value='" . $row['Schlungen'] . "'>" . $row['Schlungen'] . "</option>";
       }
      echo "</select>";
      ?>

      <?php
      error_reporting(0);
      //Drop Down for Schulung 4 
      $sql4="SELECT Schlungen  FROM schulung as s"; 
      $result=mysql_query($sql4); 
      echo "<p align='left'> <strong>Schulung 4</strong> <select name='Schlungen4'><option value default></option>";
      while ($row = mysql_fetch_array($result)) 
      {
      echo "<option value='" . $row['Schlungen'] . "'>" . $row['Schlungen'] . " </option>";
       }
      echo "</select>";
      ?>

      <?php
      error_reporting(0);
      //Drop Down for Schulung 5  
      $sql5="SELECT Schlungen  FROM schulung as s"; 
      $result=mysql_query($sql5); 
      echo "<p align='left'> <strong>Schulung 5</strong> <select name='Schlungen5'><option value default></option>";
      while ($row = mysql_fetch_array($result)) 
      {
      echo "<option value='" . $row['Schlungen'] . "'>" . $row['Schlungen'] . " </option>";
      }
      echo "</select>";
      ?>   
      <p align="left"><strong>Access_level </strong> 
      <input type="text" name="a_level" disabled="disabled">
      </p>

    

For achieving auto completion,you can create a select field in html file,call a javascript function on keyup event and use jQuery for calling your php file为了实现自动完成,您可以在 html 文件中创建一个选择字段,在 keyup 事件上调用 javascript 函数并使用 jQuery 调用您的 php 文件

<html>
<head>
    <script>
        $('.autosuggest').keyup(function(){
         $.post("<your file.php>",{any data you need},function(data){
          //echo the data
        //echo "<option value='" . $row['Schlungen'] . "'>" . //$row['Schlungen'] ."   </option>";
        $('.result').html(data)

});
        });
        $('.result option').click(function(){
            var rValue = $(this).text();
            $('.autosuggest').attr('value',rValue);        
            $('.result').html('');
        });

    </script>
</head>
<body>
    <input type='text' class='autosuggest'/>
    <select class='result'>
    </select>
</body>
</html>

try to set the value of each html select option to be the id of that text尝试将每个 html 选择选项的值设置为该文本的 id

so the echo should be所以回声应该是

echo "<option value='" . $row['id'] . "'>" . $row['Schlungen'] . " </option>";  

then on the html select add the event onchange and add the JS function foo() to fill a hidden input with all selected ids by getting them from the page using the JQuery or Javascript然后在 html 上选择添加事件onchange并添加 JS 函数 foo() 以通过使用 JQuery 或 Javascript 从页面中获取所有选定的 ID 来填充隐藏的输入

also you have to add id or class for each html select to get its selected value easily您还必须为每个 html 选择添加 id 或 class 以轻松获取其选定的值

$("#select1").val();
$("#select2").val(); ...

then insert these values in a hidden input field so the hidden input now contains all selected ids然后将这些值插入隐藏的输入字段中,以便隐藏的输入现在包含所有选定的 id

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

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