Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将选项列表中超过1个单词的完整术语输入到连续的输入字段中_Javascript_Html_Function - Fatal编程技术网

Javascript 如何将选项列表中超过1个单词的完整术语输入到连续的输入字段中

Javascript 如何将选项列表中超过1个单词的完整术语输入到连续的输入字段中,javascript,html,function,Javascript,Html,Function,我有一个html表单从数据库中读取数据。DB请求的值可以从选项列表中选择,并发送到输入字段。根据需要,可以创建其他输入字段。当所选值为单个单词时,此选项适用于以下函数: enter code here <script> // Start of function for additional input fields var counter = 1; var limit = 10; function addInput(divName){

我有一个html表单从数据库中读取数据。DB请求的值可以从选项列表中选择,并发送到输入字段。根据需要,可以创建其他输入字段。当所选值为单个单词时,此选项适用于以下函数:

enter code here

  <script>
  // Start of function for additional input fields  
     var counter = 1;

     var limit = 10;

     function addInput(divName){

          if (counter == limit)  {

               alert("You have reached the limit of adding " + counter + " 
  inputs");

          }

          else {

     // Create new div
     var newdiv = document.createElement('div');
     newdiv.innerHTML = '<br><input type="text" name="productinput[]" 
  class="awesomplete" list="productinputselect" size="20"/>';
     document.getElementById(divName).appendChild(newdiv);

     // Re instantiate Awesomplete
     new Awesomplete(newdiv.querySelector('input'), { list: 
  document.querySelector('#productinputselect') });

     // Set counter
     counter++;

          }

     }

  // End of function for additional input fields  


  // Start of function for sending selected values to input-field   

  function getSelectValues(select) {
    var result = [];
    var options = select && select.options;
    var opt;

    for (var i=0, iLen=options.length; i<iLen; i++) {
      opt = options[i];

      if (opt.selected) {
        result.push(opt.value || opt.text);
      }
    }
    return result;
   }

  function sendproductnameinput() {
    var index = 0;
          var inputs = document.formdatabaseDE.elements["productinput[]"];
      var selected = getSelectValues(document.formdatabase.productselect);

      // set the input values based on the selected options
      for (var i = 0; i < selected.length; i++) {
          if(index < inputs.length) {
              inputs[index].value = selected[i];
          index++;
          }
      }

          // empty the remaining inputs
          for (var i = selected.length; i < inputs.length; i++) {
                  inputs[i].value = '';
          }
  }

  // End of function for sending selected values to input-field   
   </script>
但是,如果选项列表中的值由两个或多个用单个空格分隔的单词组成,则只将第一个单词发送到输入字段,而不是整个术语


如果有人能帮我修改这个函数,使值完全发送到输入字段,尽管它们包含超过一个单词,我会非常高兴

这个问题的解决方法是用引号将单词除以空格,得到一个术语:

enter code here

    while ($row = mysqli_fetch_assoc($result)) { 
      echo '<option class="optproduct" value='. $row['Name'] . '>' . $row
      ['Name']. '</option>'; echo '<br>'; }
成为:

enter code here

    while ($row = mysqli_fetch_assoc($result)) { 
      echo '<option class="optproduct" value="'. $row['Name'] . '">' . $row
      ['Name']. '</option>'; echo '<br>'; }

你能给这个例子添加一个简单的pnkr/jsbin吗?@inoabrian奇怪的是,在这个工作的fiddle jsfiddle.net/586menbv/19/中,它可以正常工作!我的选项列表由以下DB查询生成:$sql=SELECT*FROM product_main ORDER by Name$结果=mysqli_查询$db,$sql;而$row=mysqli_fetch_assoc$result{echo.$row['Name'];echo;}mysqli_close$db;问题可能在这里吗?提前谢谢!现在我知道,问题只会在通过SELECT查询生成选项列表时出现,而不会在直接将值输入选项列表时出现。我认为引号可能有问题,因此只有一个词的第一个词被认为是有价值的。然而,改变报价并没有解决问题。