如果这是重复,我道歉,但我找不到任何东西。
我是Javascript的新手,我仍在尝试理解事件监听器以及代码的运行方式。
除此之外,我有一个引导模式窗口,当一个数字被放入一个输入框并张贴时,它会弹出。
弹出模态之前,该输入框上有一个自动对焦。
我在这里找到了关于如何将自动对焦放在模态窗口上的按钮上的帮助,它完全正常,如下:
$('.modal').on('shown.bs.modal', function() {
$(this).find('[autofocus]').focus();
});
我的问题是当模态窗口关闭时,我无法将自动对焦返回到输入框。
我尝试了各种版本,这两个是我的主要想法:
$("#no").click(function() {
$(this).find('[autofocus]').focus();
});
$("#no").click(function() {
$("staffID").focus();
});
其中staffID是输入框的id。
但它没有把重点放在输入框上。 #no id是模态上的id,因此如果您按否,您希望原始站点重新关注输入。
有人能指出我正确的方向吗?
也被要求提供HTML,这里是(忽略冰岛语):
<!--CLOCKIN INPUT FORM-->
<form id="staffIdForm" method="post" action="" class="clockin-form">
<input id="staffID" type="text" name="starfsmannanumer" class="clockin-box" placeholder="Starfsmannanúmer" autofocus/>
<button id="clockIn" type="button" class="btn btn-success">Stimpla</button>
</form>
<!-- WELCOME MODAL -->
<div class="modal fade" id="welcome-modal" role="dialog" aria-labelledby="welcomeModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="welcomeModalLabel">
Góðan daginn <span id="nafn"></span>
</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p>Innstimplun skráð <span id="time"></span> þann <span id="date"></span></p>
<p>Viltu skrá þig í hádegismat?</p>
</div>
<div class="modal-footer">
<button id="no" type="button" class="btn btn-secondary" data-dismiss="modal" autofocus>Nei</button>
<button id="yes" type="button" class="btn btn-success">Já</button>
</div>
</div>
</div>
</div>
以下javascript需要运行该问题:
$("#clockIn").off('click').on('click', function () {
$("#staffIdForm").submit();
});
$(function getEmployee() {
$("#staffIdForm").on('submit', function(e) {
var staffId = (document.getElementById("staffID").value);
console.log(staffId);
$.post("Attendance/Home/ClockIn", { starfsmannanumer: staffId }, function(data, status) {
console.log(data);
$("#nafn").text(data.name);
$("#clock-out-nafn").text(data.name);
$(function () {
//Display time in message
var dt = new Date();
document.getElementById("time").innerHTML = dt.toLocaleTimeString('en-GB');
document.getElementById("clock-out-time").innerHTML = dt.toLocaleTimeString('en-GB');
//Date message with Icelandic names
var monthNames = [
"janúar", "febrúar", "mars", "apríl", "maí", "júní",
"júlí", "ágúst", "september", "október", "nóvember", "desember"]
var day = dt.getDate();
var monthIndex = dt.getMonth();
document.getElementById("date").innerHTML = day + ". " + monthNames[monthIndex];;
document.getElementById("clock-out-date").innerHTML = day + ". " + monthNames[monthIndex];;
//Either check in or out message or error
if (data.userId == "-1") {
$("#invalid-staff-id").fadeIn('slow', function () {
$("#invalid-staff-id").delay(2000).fadeOut();
});
}
else if (data.timeIn != null && data.timeOut == null) {
$("#welcome-modal").stop(true).modal('show');
}
else if (data.timeIn != null && data.timeOut != null) {
$("#clock-out-message").fadeIn('slow', function() {
$("#clock-out-message").delay(5000).fadeOut();
//TODO: L�ta �tstimplunarskilabo� hverfa ef �tt er � takka
//TODO: Autofocus � 'Nei' takkann
});
}
})
document.getElementById("staffID").value = "";
});
e.preventDefault();
});
});
$('#welcome-modal').on('shown.bs.modal', function () {
$(this).find('[autofocus]').focus();
});
$('#welcome-modal').on('hidden.bs.modal', function () {
$("staffID").focus();
});
我认为这就是所需要的。
将输入焦点绑定在模态关闭上。工作解决方案将是:
$('#myModal').on('hidden.bs.modal', function () {
$('#myInput').focus();
});
另请注意,这里:
$("#no").click(function() {
$(this).find('[autofocus]').focus();
});
this
- 是你点击的元素 - 似乎不正确尝试在其中找到一些其他DOM元素