javascript 在 IE 中检测 texbox 清除事件。单击 IE 特定的清除框时,如何清除 IE (Internet Explorer) 中的输入类型 =“文本”(文本框)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15845827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 02:18:03  来源:igfitidea点击:

Detect texbox clear-event in IE. How do I clear the input type="text" (textbox) in IE (Internet Explorer) when IE-specific clear-box is clicked?

javascripttextboxclear

提问by Andrew

In Internet Explorer, there is a little x-like button is shown, when you start typing in the textbox. How do I detect the event when this icon is clicked on? Is there an event-type?

在 Internet Explorer 中,当您开始在文本框中键入时,会显示一个类似 x 的小按钮。单击此图标时如何检测事件?有事件类型吗?

<input type="text" value ="" id ="qsearch" name="qsearch"
    onBlur="qsearchLookup(this.value);" OnClick="qsearchLookup(this.value);"
    onkeyup="qsearchLookup(this.value)"  size="26">

function qsearchLookup(searchVal){
    document.getElementById("qsearch").value="";
}

enter image description here

在此处输入图片说明

采纳答案by outcoldman

I do not know about special event for this small x-like button, and I don't think that it exists, but you can use inputevent (oninput="qsearchLookup(this.value)"in your case) to catch this change.

我不知道这个类似 x 的小按钮的特殊事件,我认为它不存在,但是您可以使用输入事件(oninput="qsearchLookup(this.value)"在您的情况下)来捕捉此更改。

回答by Sunil Kumar Das

Am not sure, if still someone is looking for a solution. Unfortunately there is no event handler available for the clear(X) icon, however below code snippet/tricks worked for me. CSS to complete hide the clear(X) iconand if you don't want to hide the clear(X) iconbut need to handle then JS code snippet would help.Verified in IE 8,9,10,11 and it works fine

我不确定,是否还有人在寻找解决方案。不幸的是,clear(X) icon没有可用的事件处理程序,但是下面的代码片段/技巧对我有用。CSS 来完成隐藏clear(X) 图标,如果您不想隐藏clear(X) 图标但需要处理,那么 JS 代码片段会有所帮助。在 IE 8,9,10,11 中验证,它工作正常

CSS trick:

CSS技巧:

#textFieldId::-ms-clear {display: none;}-- For a particular text field

#textFieldId::-ms-clear {display: none;}-- 对于特定的文本字段

or

或者

input[type=text]::-ms-clear { display: none; }-- for all text fields in scope

input[type=text]::-ms-clear { display: none; }-- 适用于范围内的所有文本字段

OR

或者

JavaScript trick (to reset the text field value to empty/blank and fire the html event KeyUp. Accordingly you can change per your need)

JavaScript 技巧(将文本字段值重置为空/空白并触发 html 事件 KeyUp。因此,您可以根据需要进行更改)

$('#textFieldId').bind("mouseup", function() {
        var $input = $(this);
        var oldValue = $input.val();
        if (oldValue == "") {
            return;
        }
        setTimeout(function() {
            var newValue = $input.val();
            if (newValue == "") {
                $input.trigger("keyup");
            }
        }, 1);
    });

回答by Daniel Tonon

This worked pretty well for me :)

这对我来说效果很好:)

$input.addEventListener('mouseup', function(){
    setTimeout(function(){
        if ($input.value === '') do_stuff();
    }, 1)
});

$inputbeing a reference to the input element.

$input作为对输入元素的引用。

回答by Георги Петрунов

input.on('input propertychange',function(e){})

this works for ie10 (input) and ie8 (propertychange) but not for ie9

这适用于 ie10(输入)和 ie8(属性更改)但不适用于 ie9

i actually fixed it for ie9 with the mousedown event, not pretty ,but that and mouseup are the only events that fire on it

我实际上用 mousedown 事件为 ie9 修复了它,不是很漂亮,但是那个和 mouseup 是唯一触发它的事件