我有多个带有表单的模态框,我希望当我单击一个模态框时,它会将属性autofocus
赋予第一个visible
输入字段。因为表单中还有隐藏的输入字段。
我使用 MaterializeCSS 来生成模态框。我已经设置好了:
$('#modal-entry').modal({
onOpenEnd: function () {
alert('The autofocus needs to be added in this line');
}, // Callback for Modal open
});
最佳答案
用此替换您的警报,您当然需要表单的选择器或包含表单的容器。
$('#form input').not(":hidden").first().focus();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="form">
<input type="hidden"/>
<input type="text" placeholder="name"/>
<input type="text" placeholder="lastname"/>
</form>
关于javascript - Jquery 在单击时向字段添加自动对焦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52002899/