当我在下面的<input>标记中单击时,在单击alert()的"OK“按钮后,下面代码中的alert()将继续出现。
在单击"OK“按钮后,我需要做些什么来停止警报?我的代码需要从输入标签中取出并做一些其他的事情。
....
<input type="text" class="txtInput" value="Test for focusin event" />
....
<input type="button" id="goBtn" value="GO" />
<script>
$(".txtInput").focusin(function () {
var t = $(this).val();
alert(t);
});
<script>发布于 2017-06-27 23:47:36
如果您已经集中精力,可以使用布尔标志轨道:
....
<input type="text" class="txtInput" value="Test for focusin event" />
....
<input type="button" id="goBtn" value="GO" />
<script>
var focused=false;
$(".txtInput").focusin(function () {
if(focused) return;
focused=true;
var t = $(this).val();
alert(t);
});
<script>执行后,可以将值设置为false。
或者,您可以解除对处理程序的绑定,并在需要时重新绑定它。
发布于 2017-06-27 23:50:44
这是因为当输入聚焦时,会弹出警报,然后输入失去焦点。当您关闭警报框时,焦点返回到它之前的任何位置(输入),因此输入得到焦点,而警报弹出,.
把它处理掉。如果要调试,请使用console.log。
发布于 2017-06-27 23:48:59
焦点在input对话框关闭后返回到alert()。如果您想确保只使用alert()一次,请跟踪:
var alerted = false;
$(".txtInput").focusin(function () {
if (! alerted) {
alerted = true;
var t = $(this).val();
alert(t);
}
});或者干脆使用console.log(),完全避免alert()弹出和多焦点问题。
$(".txtInput").focusin(function () {
var t = $(this).val();
console.log(t);
});https://stackoverflow.com/questions/44791610
复制相似问题