html5 search input does not work with bootstrap

后端 未结 5 1137
既然无缘
既然无缘 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: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

    {{vm.models.listSearchResults.length}} / {{vm.data.list.length}}

    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( '' );
                        }
                    }
                });
            },
        });
    }])`
    

提交回复
热议问题