最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to capture input's type = 'text' clear event? - Stack Overflow

programmeradmin5浏览0评论
<input type='text' onchange="reportAnswer(1, this.value);" 
onkeyup="this.onchange();" onpaste="this.onchange();" 
oncut="this.onchange();" 
onclear = "this.onchange();" />

How to capture that change? This is IE11.

P.S. I added onclear handler because run out of ideas what to do.

<input type='text' onchange="reportAnswer(1, this.value);" 
onkeyup="this.onchange();" onpaste="this.onchange();" 
oncut="this.onchange();" 
onclear = "this.onchange();" />

How to capture that change? This is IE11.

P.S. I added onclear handler because run out of ideas what to do.

Share Improve this question edited Feb 3, 2023 at 12:36 Maks 113 bronze badges asked Sep 15, 2014 at 12:56 Kosmo零Kosmo零 4,15110 gold badges47 silver badges98 bronze badges 5
  • What's providing that [x]? It's not something most browsers usually provide. – T.J. Crowder Commented Sep 15, 2014 at 12:58
  • The basic idea is "clear input field value when some event happens. like click [x]". Take a look at the answer to question How do I put a clear button inside my HTML text input box like the iPhone does? – shawnzhu Commented Sep 15, 2014 at 12:59
  • @shawnzhu: The OP did say he just added onclear because he ran out of ideas. – T.J. Crowder Commented Sep 15, 2014 at 12:59
  • @T.J.Crowder - This is IE11. – Kosmo零 Commented Sep 15, 2014 at 13:00
  • @Kosmos: IE11 doesn't add that for me: jsbin./hicave/1 – T.J. Crowder Commented Sep 15, 2014 at 13:01
Add a ment  | 

3 Answers 3

Reset to default 5

You haven't said what's providing that [x], which is probably relevant, but the one event you haven't tried yet is input so: Live Example

<input type='text'
    onchange="reportAnswer(1, this.value);"
    onkeyup="this.onchange();"
    onpaste="this.onchange();"
    oncut="this.onchange();"
    oninput="this.onchange();" />

(No idea if it works, IE11 doesn't add that box for me on Windows 8.1.)

More likely, though, you need to hook into something provided by whatever it is that's providing that [x].

You can use the event of "oninput":

// Try to change value of the input below
<input id="input1" />

var input = document.getElementById("input1");
input.oninput = function(){alert("Value changed");}

so its been 8 years but had the same problem and this was the first answer to show up found the answer in to this question here: How do you detect the clearing of a "search" HTML5 input?

tl:dr

 $(some-input).on("search", function() { }); 

does the trick

发布评论

评论列表(0)

  1. 暂无评论