jquery按文本框值自动完成

时间:2013-11-18 07:44:18

标签: javascript jquery

让我们考虑textbox中的值,所以现在如果我们自动输入它 必须用逗号显示多值的单词。我试过但没有结果。

任何身体都可以解决这个问题。

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
  <script>
  $(function()
            {
               var val = document.getElementById('tags').value;
               var availableTags = val == '' : [] ? val.split(',');

    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 ).data( "ui-autocomplete" ).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>
</head>
<body>

<div class="ui-widget">
  <label for="tags">Tag programming languages: </label>
  <input id="tags" size="25" value="ACCENTURE 1,    
BPCL KORAMANGALA,
CUNNIGHAM ROAD,
HM TOWERS,  
GREATER NOIDA,  
INFOSYS 3,  
JAYANAGAR T BLOCK,  
MILLENIA,   
OZONE,  
BKC,    
FUN REPUBLIC,   
MATUNGA,
VFS UK, 
CYBER GREEN,    
PRABHADEVI, 
VRINDAVAN"/>
</div>

1 个答案:

答案 0 :(得分:0)

根据我的理解,您的 autocomplete (我已在fiddle中测试过)并未填充。原因是 ternary operator语法错误。

 var availableTags = val == '' : [] ? val.split(',');  //WRONG

将此更改为

 var availableTags = val == '' ? [] : val.split(',');  //CORRECT

看到它在JSFiddle

中工作