【问题标题】:How to add autocomplate data from a source folder?如何从源文件夹添加自动完成数据?
【发布时间】:2015-05-27 15:08:44
【问题描述】:

这只是jqueryui自动完成的代码。

        <script>
          $(function() {
            var availableTags = [
              "ActionScript",
              "AppleScript",
              "Asp",
              "BASIC",
              "C",
              "C++",
              "Clojure",
              "COBOL",
              "ColdFusion",
              "Erlang",
              "Fortran",
              "Groovy",
              "Haskell",
              "Java",
              "JavaScript",
              "Lisp",
              "Perl",
              "PHP",
              "Python",
              "Ruby",
              "Scala",
              "Scheme"
            ];
            function split( val ) {
              return val.split( /,\s*/ );
            }
            function extractLast( term ) {
              return split( term ).pop();
            }

            $( "#tags" )
              // don't navigate away from the field on tab when selecting an item
              .bind( "keydown", function( event ) {
                if ( event.keyCode === $.ui.keyCode.TAB &&
                    $( this ).autocomplete( "instance" ).menu.active ) {
                  event.preventDefault();
                }
              })
              .autocomplete({
                minLength: 0,
                source: function( request, response ) {
                  // delegate back to autocomplete, but extract the last term
                  response( $.ui.autocomplete.filter(
                    availableTags, extractLast( request.term ) ) );
                },
                focus: function() {
                  // prevent value inserted on focus
                  return false;
                },
                select: function( event, ui ) {
                  var terms = split( this.value );
                  // remove the current input
                  terms.pop();
                  // add the selected item
                  terms.push( ui.item.value );
                  // add placeholder to get the comma-and-space at the end
                  terms.push( "" );
                  this.value = terms.join( ", " );
                  return false;
                }
              });
          });
          </script>




<div class="ui-widget">
      <label for="tags">Tag programming languages: </label>
      <input id="tags" size="50">
    </div>

如何从文件夹添加自动完成数据? 例如,我想将图像名称添加为文件夹中的自动完成值。

 <script>
          $(function() {
            var availableTags = [
             //images_name
            ];
            function split( val ) {
              return val.split( /,\s*/ );
            }
            function extractLast( term ) {
              return split( term ).pop();
            }

            $( "#tags" )
              // don't navigate away from the field on tab when selecting an item
              .bind( "keydown", function( event ) {
                if ( event.keyCode === $.ui.keyCode.TAB &&
                    $( this ).autocomplete( "instance" ).menu.active ) {
                  event.preventDefault();
                }
              })
              .autocomplete({
                minLength: 0,
                source: function( request, response ) {
                  // delegate back to autocomplete, but extract the last term
                  response( $.ui.autocomplete.filter(
                    availableTags, extractLast( request.term ) ) );
                },
                focus: function() {
                  // prevent value inserted on focus
                  return false;
                },
                select: function( event, ui ) {
                  var terms = split( this.value );
                  // remove the current input
                  terms.pop();
                  // add the selected item
                  terms.push( ui.item.value );
                  // add placeholder to get the comma-and-space at the end
                  terms.push( "" );
                  this.value = terms.join( ", " );
                  return false;
                }
              });
          });
          </script>




<div class="ui-widget">
      <label for="tags">Tag programming languages: </label>
      <input id="tags" size="50">
    </div>

如果我使用 $getJSON 将文件/图像名称作为数组获取的代码是什么?有没有这样的方法?

【问题讨论】:

    标签: javascript php jquery jquery-ui jquery-autocomplete


    【解决方案1】:
    <script>
    
     var available_tags=[<?php 
     foreach(glob('../image/imagefiles/*.*') as $key=>$filename){
    
          if($key==0)
          {echo "'".$filename."'";}
          else
          {echo ",'".$filename."'";}
     }
    
    
    
     ?>]
    
    
    </script> 
    

    【讨论】:

    • 不工作。自动完成的输出未显示。
    • 更改目录路径,先检查available_tags是否包含数据
    • 我从var availableTags = [&lt;?php foreach(glob('../image/imagefiles/*.*') as $filename){ echo $filename ; } ?&gt;];var availableTags = ["&lt;?php foreach(glob('../image/imagefiles/*.*') as $filename){ echo $filename ; } ?&gt;",];,但它在同一行/单行中显示了我的值..
    • 感谢您的回答但仍无法正常工作需要输出" ", 但您的输出' ", '
    • 我意识到我的错误...已经更新了答案...再次检查上次:)
    猜你喜欢
    • 2014-07-28
    • 1970-01-01
    • 1970-01-01
    • 2020-12-14
    • 1970-01-01
    • 1970-01-01
    • 2010-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多