【问题标题】:text input with clear button which auto hide and show带有自动隐藏和显示的清除按钮的文本输入
【发布时间】:2017-05-21 17:01:17
【问题描述】:

我在一个网站上工作,我使用了一个搜索框,在该搜索输入字段中我想设置一个重置按钮“X”

因此,当搜索框输入字段为空时,它不会显示“X”。但是当用户在字段中输入任何内容时,“X”会自动出现。

再一次,当用户点击“X”时,它会清除所有输入的数据并仍然专注于输入字段?

到目前为止,我已经做到了

只有 JAVASCRIPT 代码我不使用 jQuery!

var searchSminput = document.getElementById("offcansearch").value.length;

if (searchSminput == 0) {
document.getElementById("resetb").style.display = "none";
} else {
document.getElementById("resetb").style.display = "block";
}
	#resetb {
		
		background-image: url(http://cdn.onlinewebfonts.com/svg/img_286433.svg);
		background-repeat: no-repeat;
		background-size: 22px 42px;
		cursor: pointer;
		display: block;
		transition: all 300ms ease;
		width: 22px;
		height: 42px;
		border: none;
		background-color: transparent;
		outline: none;
		right: 10px;
		top: 5px;
		position: absolute;
		margin: 0;
		padding: 0;
	}	
  #offcansearch {
  width: 100%;
  height: 30px;
  }
<form>
<input id="offcansearch" type="text" name="search" placeholder="Search">
<button id="resetb" type="reset"></button>
</form>

【问题讨论】:

    标签: javascript html


    【解决方案1】:

    向输入添加更改事件:

    document.getElementById('offcansearch').addEventListener('change', function() { ... }
    

    还添加到重置按钮(点击隐藏按钮):

    resetb.addEventListener('click', function() {
      resetb.style.display = "none";
    });
    

    var resetb =  document.getElementById("resetb");
    var offcansearch = document.getElementById('offcansearch');
    
    offcansearch.addEventListener('keyup', function() {
      var searchSminput = offcansearch.value.length;
      if (searchSminput == 0) {
        resetb.style.display = "none";
      } else {
        resetb.style.display = "block";
      }
    });
    
    resetb.addEventListener('click', function() {
      resetb.style.display = "none";
      offcansearch.focus();
    });
    #resetb {
      background-image: url(http://cdn.onlinewebfonts.com/svg/img_286433.svg);
      background-repeat: no-repeat;
      background-size: 22px 42px;
      cursor: pointer;
      display: none;
      transition: all 300ms ease;
      width: 22px;
      height: 42px;
      border: none;
      background-color: transparent;
      outline: none;
      right: 10px;
      top: 5px;
      position: absolute;
      margin: 0;
      padding: 0;
    }
    
    #offcansearch {
      width: 100%;
      height: 30px;
    }
    <form>
      <input id="offcansearch" type="text" name="search" placeholder="Search">
      <button id="resetb" type="reset"></button>
    </form>

    【讨论】:

    • 优秀的遮阳篷。我只需将事件更改为keyupkeydown 而不是changechange 事件仅在输入失去焦点时发生。
    • 我必须输入 2 个单词,然后每次我输入时它都在工作,而不是工作!出事了!请查看帮助
    • 是的,现在它开始工作了!先生,只剩下一件事了。当我们单击清除按钮“X”以清除文本时。然后焦点应该在搜索框内没有消失!
    • @DanielH thx 是的,它现在正在工作实际上我试图从 2 小时开始解决这个问题,但有些我无法解决这个问题。所以我完全感到沮丧。无论如何再次感谢
    • @ArchiPatel 酷,尽可能接受答案,祝编码好运!
    猜你喜欢
    • 2020-05-05
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-07
    • 2012-07-30
    • 2018-01-22
    相关资源
    最近更新 更多