How do I put a clear button inside my HTML text input box like the iPhone does?

前端 未结 10 1997
太阳男子
太阳男子 2020-12-02 04:21

I want to have a nice little icon that, when clicked will clear the text in the box.

This is to save space rather than having a clear link outside of t

相关标签:
10条回答
  • 2020-12-02 05:18

    HTML5 introduces the 'search' input type that I believe does what you want.

    <input type="search" />

    Here's a live example.

    0 讨论(0)
  • 2020-12-02 05:21

    Check out our jQuery-ClearSearch plugin. It's a configurable jQuery plugin - adapting it to your needs by styling the input field is straightforward. Just use it as follows:

    <input class="clearable" type="text" placeholder="search">
    
    <script type="text/javascript">
        $('.clearable').clearSearch();
    </script>
    

    ​ Example: http://jsfiddle.net/wldaunfr/FERw3/

    0 讨论(0)
  • 2020-12-02 05:23

    @thebluefox has summarized the most of all. You're only also forced to use JavaScript to make that button to work anyway. Here's an SSCCE, you can copy'n'paste'n'run it:

    <!DOCTYPE html>
    <html lang="en">
        <head>
            <title>SO question 2803532</title>
            <script src="http://code.jquery.com/jquery-latest.min.js"></script>
            <script>
                $(document).ready(function() {
                    $('input.deletable').wrap('<span class="deleteicon" />').after($('<span/>').click(function() {
                        $(this).prev('input').val('').trigger('change').focus();
                    }));
                });
            </script>
            <style>
                span.deleteicon {
                    position: relative;
                }
                span.deleteicon span {
                    position: absolute;
                    display: block;
                    top: 5px;
                    right: 0px;
                    width: 16px;
                    height: 16px;
                    background: url('http://cdn.sstatic.net/stackoverflow/img/sprites.png?v=4') 0 -690px;
                    cursor: pointer;
                }
                span.deleteicon input {
                    padding-right: 16px;
                    box-sizing: border-box;
                }
            </style>
        </head>
        <body>
            <input type="text" class="deletable">
        </body>
    </html>
    

    Live example here.

    jQuery is by the way not necessary, it just nicely separates the logic needed for progressive enhancement from the source, you can of course also go ahead with plain HTML/CSS/JS:

    <!DOCTYPE html>
    <html lang="en">
        <head>
            <title>SO question 2803532, with "plain" HTML/CSS/JS</title>
            <style>
                span.deleteicon {
                    position: relative;
                }
                span.deleteicon span {
                    position: absolute;
                    display: block;
                    top: 5px;
                    right: 0px;
                    width: 16px;
                    height: 16px;
                    background: url('http://cdn.sstatic.net/stackoverflow/img/sprites.png?v=4') 0 -690px;
                    cursor: pointer;
                }
                span.deleteicon input {
                    padding-right: 16px;
                    box-sizing: border-box;
                }
            </style>
        </head>
        <body>
            <span class="deleteicon">
                <input type="text">
                <span onclick="var input = this.previousSibling; input.value = ''; input.focus();"></span>
            </span>
        </body>
    </html>
    

    You only ends up with uglier HTML (and non-crossbrowser compatible JS ;) ).

    Note that when the UI look'n'feel isn't your biggest concern, but the functionality is, then just use <input type="search"> instead of <input type="text">. It'll show the (browser-specific) clear button on HTML5 capable browsers.

    0 讨论(0)
  • 2020-12-02 05:27

    Maybe this simple solution can help:

    <input type="text" id="myInput" value="No War"/><button onclick="document.getElementById('myInput').value = ''" title="Clear">X</button></input>

    0 讨论(0)
提交回复
热议问题