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

前端 未结 10 2007
太阳男子
太阳男子 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:05

    Nowadays with HTML5, it's pretty simple:

    <input type="search" placeholder="Search..."/>
    

    Most modern browsers will automatically render a usable clear button in the field by default.

    (If you use Bootstrap, you'll have to add an override to your css file to make it show)

    input[type=search]::-webkit-search-cancel-button {
        -webkit-appearance: searchfield-cancel-button;
    }
    

    Safari/WebKit browsers can also provide extra features when using type="search", like results=5 and autosave="...", but they also override many of your styles (e.g. height, borders) . To prevent those overrides, while still retaining functionality like the X button, you can add this to your css:

    input[type=search] {
        -webkit-appearance: none;
    }
    

    See css-tricks.com for more info about the features provided by type="search".

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

    You can't actually put it inside the text box unfortunately, only make it look like its inside it, which unfortunately means some css is needed :P

    Theory is wrap the input in a div, take all the borders and backgrounds off the input, then style the div up to look like the box. Then, drop in your button after the input box in the code and the jobs a good'un.

    Once you've got it to work anyway ;)

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

    It is so simple in HTML5

    <input type="search">
    

    This will do your job!

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

    Of course the best approach is to use the ever-more-supported <input type="search" />.

    Anyway for a bit of coding fun I thought that it could be achieved also using the form's reset button, and this is the working result (it worths notings that cannot live other inputs but the search field with this approach, or the reset button will erase them too), no javascript needed:

    form > div{
        position: relative;
        width: 200px;
    }
    
    form [type="text"] {
        width: 100%;
        padding-right: 20px;
    }
    
    form [type="reset"] {
        position: absolute;
        border: none;
        display: block;
        width: 16px;
        border-radius: 20px;
        top: 2px;
        bottom: 2px;
        right: -20px;
        background-color: #ffffd;
        padding: 0px;
        margin: 0px;
    }
    <form>
    	<div>
    		<input type="text" />
    		<input type="reset" value="X" />
    	</div>
    </form>

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

    I got a creative solution I think you are looking for

    $('#clear').click(function() {
      $('#input-outer input').val('');
    });
    body {
      font-family: "Tahoma";
    }
    #input-outer {
      height: 2em;
      width: 15em;
      border: 1px #e7e7e7 solid;
      border-radius: 20px;
    }
    #input-outer input {
      height: 2em;
      width: 80%;
      border: 0px;
      outline: none;
      margin: 0 0 0 10px;
      border-radius: 20px;
      color: #666;
    }
    #clear {
      position: relative;
      float: right;
      height: 20px;
      width: 20px;
      top: 5px;
      right: 5px;
      border-radius: 20px;
      background: #f1f1f1;
      color: white;
      font-weight: bold;
      text-align: center;
      cursor: pointer;
    }
    #clear:hover {
      background: #ccc;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="input-outer">
      <input type="text">
      <div id="clear">
        X
      </div>
    </div>

    https://jsfiddle.net/qdesign/xn9eogmx/1/

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

    @Mahmoud Ali Kaseem

    I have just changed some CSS to make it look different and added focus();

    https://jsfiddle.net/xn9eogmx/81/

    $('#clear').click(function() {
      $('#input-outer input').val('');
      $('#input-outer input').focus();
    });
    body {
      font-family: "Arial";
      font-size: 14px;
    }
    #input-outer {
      height: 2em;
      width: 15em;
      border: 1px #777 solid;
      position: relative;
      padding: 0px;
      border-radius: 4px;
    }
    #input-outer input {
      height: 100%;
      width: 100%;
      border: 0px;
      outline: none;
      margin: 0 0 0 0px;
      color: #666;
      box-sizing: border-box;
      padding: 5px;
      padding-right: 35px;
      border-radius: 4px;
    }
    #clear {
      position: absolute;
      float: right;
      height: 2em;
      width: 2em;
      top: 0px;
      right: 0px;
      background: #aaa;
      color: white;
      text-align: center;
      cursor: pointer;
      border-radius: 0px 4px 4px 0px;
    }
    #clear:after {
      content: "\274c";
      position: absolute;
      top: 4px;
      right: 7px;
    }
    #clear:hover,
    #clear:focus {
      background: #888;
    }
    #clear:active {
      background: #666;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="input-outer">
      <input type="text">
      <div id="clear"></div>
    </div>

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