html5 search input does not work with bootstrap

后端 未结 5 1125
既然无缘
既然无缘 2020-12-09 02:12

I have tested the input[type=\"search\"] and it does not show the clear (x) icon when the bootstrap styles have been applied.

相关标签:
5条回答
  • 2020-12-09 02:33

    Change your input type to 'search'instead of 'text' and -webkit-appearance: block instead of 'none'

    0 讨论(0)
  • 2020-12-09 02:34

    If you're using Web Kit your problem may be related to what sk8terboi87 posted.

    Bootstrap doesn't support styling the Search type inputs for you as it's too difficult to do reliably in Web Kit.

    Bootstrap uses a reset CSS which removes the cross that usually appears, you can get it back by modifying the core CSS, but this could cause issues in the future when upgrading.

    If it's happening in other browsers though it could be another issue.

    0 讨论(0)
  • 2020-12-09 02:41

    An issue related to yours has been already posted in their github https://github.com/twbs/bootstrap/issues/5624

    0 讨论(0)
  • 2020-12-09 02:44
    input[type="search"] {
     -webkit-box-sizing: content-box;
     -moz-box-sizing: content-box;
     box-sizing: content-box;
     -webkit-appearance: searchfield;
    }
    
    input[type="search"]::-webkit-search-cancel-button {
    -webkit-appearance: searchfield-cancel-button;
    }
    

    works for me on Chrome (on my Mac, but not on my iPhone...)

    0 讨论(0)
  • 2020-12-09 02:47

    To have the same user experience across all devices, I ended up writing an Angular directive that I put on Bootstrap 'input-group-btn's.

    Angular view HTML

    <div class="input-group">
        <input type="search" id="listItemSearch" class="form-control"
            placeholder="Search text..." 
            title="Search" aria-label="Search"
            data-ng-model-options="{'debounce':1500, 'updateOn':'blur default'}"
            data-ng-model="vm.filters.listItemSearchText"/>
        <span class="input-group-btn">
            <button type="button" class="btn btn-default" 
                data-ng-disabled="!vm.filters.listItemSearchText" 
                aria-describedby="listItemSearch" 
                data-clear-search-by-aria="#listItemSearch">
                <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
            </button>
        </span>
        <span class="input-group-addon">
            <span>{{vm.models.listSearchResults.length}}</span>
            <span>/</span>
            <span>{{vm.data.list.length}}</span>
        </span>
    </div>
    

    Angular directive JavaScript

    .directive( 'clearSearchByAria', ['$parse', function clearSearchByAria( $parse )
    {
        return(
        {
            'restrict':'A',
            'link':function clearSearchByAria_link( scope, jqElem, ngAttr )
            {
                jqElem.on( 'click', function( $event )
                {
                    var $clickedElem = angular.element($event.currentTarget);
                    var $searchInput;
    
                    // Specified by selector.
                    if( !!ngAttr.clearSearchByAria )
                    {
                        var $specifiedElem = angular.element(ngAttr.clearSearchByAria)
                        if( $specifiedElem.length == 1 )
                        {$searchInput = $specifiedElem;}
                    }
                    else
                    {
                        var $describedElem = $clickedElem.find( '[aria-describedby]' ).addBack( '[aria-describedby]' );
                        // Specified by 'aria-describedby'.
                        if( $describedElem.length == 1 )
                        {$searchInput = angular.element(''.concat( '#', $describedElem.attr('aria-describedby')));}
                        else
                        {
                            throw( new ReferenceError( "'clear-search-by-aria' attributes requires either 1) to be empty and for the element (or a descendant) to have an 'aria-describedby' attribute referring to another element or 2) to specify an element selector (e.g. '#id') to another element." ));
                        }
                    }
    
                    if( !!$searchInput && $searchInput.length == 1 )
                    {
                        var ng_model = $searchInput.data( 'ngModel' ) || $searchInput.attr( 'ng-model' );
                        if( !!ng_model )
                        {
                            var modelGetter = $parse( ng_model );
                            modelGetter.assign( scope, '' );
                            scope.$apply();
                        }
                        else
                        {
                            $searchInput.val( '' );
                        }
                    }
                });
            },
        });
    }])`
    
    0 讨论(0)
提交回复
热议问题