2017-05-22 79 views
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”。

我该怎么做?

回答

1

官方的引导文件给出了只是它 http://getbootstrap.com/javascript/#modals

从文档

$('#myModal').on('shown.bs.modal', function() { 
    $('#myInput').focus() 
}) 

示例的示例使用BoostrapDialog类同样的事情会像

onshown: function(dialogRef){ 
    $('#myInput').focus(); 
} 

因此,最终的代码将看起来像

if (instanceDialogClientDonneur == null ) { 
    instanceDialogClientDonneur = new BootstrapDialog.show({ 
     title : titre, 
     cssClass : 'lieu-dialog', 
     modal : true, 

     closeOnEscape : true, 
     onhidden : function() {    
      instanceDialogClientDonneur =null; 
     }, 
     onshown: function(){ 
      $('#code_recherche').focus(); 
     } 
     message : enteteZoneRecherche + resTab +"</div>" 
    }); 
} 
+0

谢谢你的帮助! 您的代码正常工作,谢谢! 当BootstrapDialog出现时,输入元素可能有一个值。在这种情况下,你如何将光标放到最后,而不是像当前那样开始? – Grichka

+0

这不是'focus'的默认行为。默认情况下,它将光标移动到文本的末尾。您的代码中必须有其他内容导致此效果 –

+0

我加了这个,它工作正常: \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

相关问题