【问题标题】:Java Script of autosearch is not working on textbox with CSS自动搜索的 Java 脚本不适用于带有 CSS 的文本框
【发布时间】:2015-09-23 07:45:14
【问题描述】:

我想要一个带有自动搜索 JavaScript 的文本框。我的 Aspx 文本框是:

 <asp:TextBox ID="txt_DestinationRoute" runat="server" CssClass="form-control" placeholder="e.g. Route you followed" TextMode="MultiLine" Rows="2"></asp:TextBox>
  <asp:RequiredFieldValidator ValidationGroup="destinations" ID="rfvtxt_DestinationRoute" ControlToValidate="txt_DestinationRoute" Display="Dynamic" runat="server" ErrorMessage="*" ForeColor="Red"></asp:RequiredFieldValidator>

我的 JavaScript 是:

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false&libraries=places"></script>
    <script type="text/javascript">
        google.maps.event.addDomListener(window, 'load', function () {
            var places = new google.maps.places.Autocomplete(document.getElementById('txt_DestinationRoute'));
            google.maps.event.addListener(places, 'place_changed', function () {
                var place = places.getPlace();
                var address = place.formatted_address;
                var latitude = place.geometry.location.A;
                var longitude = place.geometry.location.F;
                var mesg = "Address: " + address;
                mesg += "\nLatitude: " + latitude;
                mesg += "\nLongitude: " + longitude;
                //document.getElementById('lat').innerHTML = latitude;
                //document.getElementById('log').innerHTML = longitude;
                alert(mesg);
            });
        });
    </script>

当我尝试使用没有任何 CSS 类的普通文本框时效果很好 但它不适用于 CSS。

【问题讨论】:

    标签: javascript css asp.net autocomplete


    【解决方案1】:

    问题是asp为客户端生成不同的id,试试:

    <asp:TextBox ClientIDMode="Static" 
    

    还要在代码检查器中查看生成的文本框。

    否则你可以试试这个:

       var places = new google.maps.places.Autocomplete(document.getElementById('<%= txt_DestinationRoute.ClientID %>'));
    

    【讨论】:

      【解决方案2】:

      请在下面找到答案

      用户界面

      <div class="ui-widget">
          <label for="birds">Birds: </label>
          <input id="birds" size="50" />
          <input type="button" id="search-trigger" value="Trigger" />
      </div>
      
      
        **Script**
      
      <script type="text/javascript">
      $(function() {
          function split( val ) {
              return val.split( /,\s*/ );
          }
          function extractLast( term ) {
              return split( term ).pop();
          }
      
          // Button listener that triggs the search event
          $("#search-trigger").click(function(){
              var searchTerm = $( "#birds" ).val();
              $( "#birds" ).autocomplete( "search" , searchTerm);
          });
      
      
          $( "#birds" )               
              .bind( "keydown", function( event ) {
                  if ( event.keyCode === $.ui.keyCode.TAB &&
                      $( this ).data( "autocomplete" ).menu.active ) {
                      event.preventDefault();
                  }
              })
              .autocomplete({
                  source: ["lorem", "ipsum", "dolor"], 
                      /* Commented this out to use dummy data above
                                  function( request, response ) {
                      $.getJSON( "search.php", {
                          term: extractLast( request.term )
                      }, response );
                  },*/
                  search: function() {
                      var term = extractLast( this.value );
      
                                /* What is this check for?
                      if ( term.length < 2 ) {
      
      
       return false;
                      }*/
                  },
                  focus: function() {
                      return false;
                  },
                  select: function( event, ui ) {
                      var terms = split( this.value );
                      terms.pop();
                      terms.push( ui.item.value );
                      terms.push( "" );
                      this.value = terms.join( ", " );
                      return false;
                  },
                  //Added for "show all" support
                  minLength: 0
              });
      });
      </script>
      

      【讨论】:

      • 我在文本框 css 上遇到了问题,而不是在脚本上......但感谢新方法,这也是一个好方法......
      • 感谢您的反馈 :)
      猜你喜欢
      • 2020-06-15
      • 2012-01-09
      • 2021-07-09
      • 1970-01-01
      • 1970-01-01
      • 2019-12-09
      • 1970-01-01
      • 1970-01-01
      • 2020-10-01
      相关资源
      最近更新 更多