18910140161

JavaScript-Clear IE堆栈溢出时文本输入中的图标事件

顺晟科技

2022-10-19 14:38:45

150

构成货币转换器,一个带有要转换的数字,另一个接收转换后的数字。

我的问题是:在IE中的输入字段中,会出现一个X,如果单击它,则允许删除该值。我需要知道如何完成(可能使用Javascript)在单击X时,我必须删除在其他输入字段中收到的结果(请参见图像)。


顺晟科技:

清除(X)图标没有可用的特定事件处理程序。作为一种解决办法,您可以使用mouseup event在单击clear(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>

结果:

  • TAG:
相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航