这在 chrome 和 IE11 中对我有用。似乎微调器小部件在创建后没有设置焦点。如果您开始进行部分回发,则使用“开”是一种很好的做法。然而,在 Firefox 中,即使有额外的调用,它也没有设置焦点。这是一个在论坛中详细讨论的问题。如果你需要 FF,你将不得不处理它。
但是,FF 问题仅在用户从不单击微调器时才会出现问题。我不确定该解决方案是否已准备好用于生产。
$("body").on("focus", ".spinner", function () {
$(this).spinner({
create: function (event, ui) {
$(this).focus();
// Trying (and failing) to deal with FF
/*setTimeout(function () {
$(this).focus()
}, 10); */
}
});
});
$("body").on("blur", ".spinner", function () {
$(this).spinner("destroy");
});
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet" />
<input type="text" class="spinner" /><br>
<input type="text" class="spinner" /><br>
<input type="text" class="spinner" /><br>
<input type="text" class="spinner" /><br>