【问题标题】:Clear icon event in text input on IE在 IE 上的文本输入中清除图标事件
【发布时间】:2021-11-25 12:02:57
【问题描述】:
组成一个货币转换器,一个带有要转换的数字,另一个接收转换后的数字。
我的问题是:在 IE 中的输入字段中,如果单击允许删除值,则会出现一个 X。我需要知道在单击 X 时我是如何做到的(可能使用 Javascript)我必须删除在另一个输入字段中收到的结果(见图)。
【问题讨论】:
标签:
javascript
html
internet-explorer
【解决方案1】:
没有可用于 clear(X) 图标的特定事件处理程序。作为一种解决方法,您可以使用mouseup event 在通过单击清除 (X) 图标清除输入时捕捉更改。
示例代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
input1: <input type="text" id="ipt1" value="111" /><br />
input2: <input type="text" id="ipt2" value="222" />
<script>
$("#ipt1").bind("mouseup", function (e) {
var $input = $(this),
oldValue = $input.val();
if (oldValue == "") return;
// When this event is fired after clicking on the clear button
// the value is not cleared yet. We have to wait for it.
setTimeout(function () {
var newValue = $input.val();
if (newValue == "") {
$("#ipt2").val("");
}
}, 1);
});
</script>
</body>
</html>
结果: