How do I clear a search box with an 'x' in bootstrap 3?

前端 未结 9 1532
情歌与酒
情歌与酒 2020-12-12 10:44

Having some trouble with bootstrap, so some help would be awesome. Thanks

What I would like:(top part)

\

相关标签:
9条回答
  • 2020-12-12 11:13

    Here is my working solution with search and clear icon for Angularjs\Bootstrap with CSS.

        <div class="form-group has-feedback has-clear">
                        <input type="search" class="form-control removeXicon" ng-model="filter" style="max-width:100%" placeholder="Search..." />
                        <div ng-switch on="!!filter">
                            <div ng-switch-when="false">
                                <span class="glyphicon glyphicon-search form-control-feedback"></span>
                            </div>
                            <div ng-switch-when="true">
                                <span class="glyphicon glyphicon-remove-sign form-control-feedback" ng-click="$parent.filter = ''" style="pointer-events: auto; text-decoration: none;cursor: pointer;"></span>
                            </div>
                        </div>                        
                    </div>
    
    ------
    
    CSS
    
    /* hide the built-in IE10+ clear field icon */
    .removeXicon::-ms-clear {
      display: none;
    }
    
    /* hide the built-in chrome clear field icon */
    .removeXicon::-webkit-search-decoration,
    .removeXicon::-webkit-search-cancel-button,
    .removeXicon::-webkit-search-results-button,
    .removeXicon::-webkit-search-results-decoration { 
          display: none; 
    }
    
    0 讨论(0)
  • 2020-12-12 11:14

    Do it with inline styles and script:

    <div class="btn-group has-feedback has-clear">
        <input id="searchinput" type="search" class="form-control" style="width:200px;">
            <a 
    id="searchclear" 
    class="glyphicon glyphicon-remove-circle form-control-feedback form-control-clear" 
    style="pointer-events:auto; text-decoration:none; cursor:pointer;"
    onclick="$(this).prev('input').val('');return false;">
    </a>
    </div>
    
    0 讨论(0)
  • 2020-12-12 11:19

    To get something like this

    input with clear button

    with Bootstrap 3 and Jquery use the following HTML code:

    <div class="btn-group">
      <input id="searchinput" type="search" class="form-control">
      <span id="searchclear" class="glyphicon glyphicon-remove-circle"></span>
    </div>
    

    and some CSS:

    #searchinput {
        width: 200px;
    }
    #searchclear {
        position: absolute;
        right: 5px;
        top: 0;
        bottom: 0;
        height: 14px;
        margin: auto;
        font-size: 14px;
        cursor: pointer;
        color: #ccc;
    }
    

    and Javascript:

    $("#searchclear").click(function(){
        $("#searchinput").val('');
    });
    

    Of course you have to write more Javascript for whatever functionality you need, e.g. to hide the 'x' if the input is empty, make Ajax requests and so on. See http://www.bootply.com/121508

    0 讨论(0)
  • 2020-12-12 11:24

    Thanks unwired your solution was very clean. I was using horizontal bootstrap forms and made a couple modifications to allow for a single handler and form css.

    html: - UPDATED to use Bootstrap's has-feedback and form-control-feedback

     <div class="container">
      <form class="form-horizontal">
       <div class="form-group has-feedback">
        <label for="txt1" class="col-sm-2 control-label">Label 1</label>
        <div class="col-sm-10">
         <input id="txt1" type="text" class="form-control hasclear" placeholder="Textbox 1">
         <span class="clearer glyphicon glyphicon-remove-circle form-control-feedback"></span>
        </div>
       </div>
       <div class="form-group has-feedback">
        <label for="txt2" class="col-sm-2 control-label">Label 2</label>
        <div class="col-sm-10">
          <input id="txt2" type="text" class="form-control hasclear" placeholder="Textbox 2">
          <span class="clearer glyphicon glyphicon-remove-circle form-control-feedback"></span>
        </div>
       </div>
       <div class="form-group has-feedback">
        <label for="txt3" class="col-sm-2 control-label">Label 3</label>
        <div class="col-sm-10">
         <input id="txt3" type="text" class="form-control hasclear" placeholder="Textbox 3">
         <span class="clearer glyphicon glyphicon-remove-circle form-control-feedback"></span>
        </div>   
       </div>
      </form>
     </div>
    

    javascript:

    $(".hasclear").keyup(function () {
        var t = $(this);
        t.next('span').toggle(Boolean(t.val()));
    });
    $(".clearer").hide($(this).prev('input').val());
    $(".clearer").click(function () {
        $(this).prev('input').val('').focus();
        $(this).hide();
    });
    

    example: http://www.bootply.com/130682

    0 讨论(0)
  • 2020-12-12 11:25

    AngularJS / UI-Bootstrap Answer

    • Use Bootstrap's has-feedback class to show an icon inside the input field.
    • Make sure the icon has style="cursor: pointer; pointer-events: all;"
    • Use ng-click to clear the text.

    JavaScript (app.js)

    var app = angular.module('plunker', ['ui.bootstrap']);
    
    app.controller('MainCtrl', function($scope) {
    
      $scope.params = {};
    
      $scope.clearText = function() {
        $scope.params.text = null;
      }
    
    });
    

    HTML (index.html snippet)

          <div class="form-group has-feedback">
            <label>text box</label>
            <input type="text"
                   ng-model="params.text"
                   class="form-control"
                   placeholder="type something here...">
            <span ng-if="params.text"
                  ng-click="clearText()"
                  class="glyphicon glyphicon-remove form-control-feedback" 
                  style="cursor: pointer; pointer-events: all;"
                  uib-tooltip="clear">
            </span>
          </div>
    

    Here's the plunker: http://plnkr.co/edit/av9VFw?p=preview

    0 讨论(0)
  • 2020-12-12 11:25

    Place the image (cancel icon) with position absolute, adjust top and left properties and call method onclick event which clears the input field.

    <div class="form-control">
        <input type="text" id="inputField" />
    </div>
    <span id="iconSpan"><img src="icon.png" onclick="clearInputField()"/></span>
    

    In css position the span accordingly,

    #iconSpan {
     position : absolute;
     top:1%;
     left :14%;
    }
    
    0 讨论(0)
提交回复
热议问题