0
我有一个带有输入元素和其他东西的BootstrapDialog,并且当BootstrapDialog出现时我需要在输入元素上设置焦点。如何在BootstrapDialog中的输入元素上设置焦点
这里是我的代码:
var instanceDialogClientDonneur = null;
...
if (instanceDialogClientDonneur == null ) {
instanceDialogClientDonneur = new BootstrapDialog.show({
title : titre,
cssClass : 'lieu-dialog',
modal : true,
closeOnEscape : true,
onhidden : function() {
instanceDialogClientDonneur =null;
},
message : enteteZoneRecherche + resTab +"</div>"
});
} else {
...
}
enteteZoneRecherche是这样的:
<div class="row"><div class="input-group"><span class="input-group-addon">Rechercher</span><input type="text" id="code_recherche" value="" onkeyup="completerClient(event,this.id,'clientnom','Liste des clients','remplirZonesClientAdrEnvoi','',true);" class="form-control" placeholder="Tapez un code..."></div>
和resTab:
<div id="tableauResultat" class="row" style="height:350px;overflow-y: scroll"><table class="table table-bordered table-hover"><tr><th>Code</th><th>Nom</th><th>Ville</th></tr><tr id="idTr_1106" onclick="remplirZonesClientAdrEnvoi(this.id);"><td>000006</td><td>ARSENE</td><td>Brest</td></tr><tr id="idTr_1107" onclick="remplirZonesClientAdrEnvoi(this.id);"><td>100004</td><td>ANQUETIL</td><td>BRIVES</td></tr></table></div>
我需要将焦点设置 “code_recherche”。
我该怎么做?
谢谢你的帮助! 您的代码正常工作,谢谢! 当BootstrapDialog出现时,输入元素可能有一个值。在这种情况下,你如何将光标放到最后,而不是像当前那样开始? – Grichka
这不是'focus'的默认行为。默认情况下,它将光标移动到文本的末尾。您的代码中必须有其他内容导致此效果 –
我加了这个,它工作正常: \t var inputElement = document.getElementById('code_recherche'); \t var inputElementLength = inputElement.value.length; \t inputElement.selectionStart = inputElementLength; \t inputElement.selectionEnd = inputElementLength; \t inputElement.focus(); – Grichka