2014-09-24 26 views
1

我試圖在模態顯示之後將注意力集中在輸入上。如何將重點放在模態輸入上?

到目前爲止我見過並嘗試過的解決方案和鏈接:How to focus on a form input text field on page load using jQuery?Jquery Focus on input field,並且它們都不起作用。

我的代碼其實很簡單:

$input = $("#letter-change-text").find("input[type='text']"); 
$input.val($.trim("I am just testing!!")); 
$("#letter-change-text").modal('show'); 
$input.focus(); // this should focus the textbox in order to allow immediately input by the user 

可以在JSfiddle檢查這個問題。

我也沒有任何成功的嘗試:

setTimeout(function(){ 
    $input.focus(); 
}, 0); 
+0

http://jsfiddle.net/nce5aau8/1/ – adeneo 2014-09-24 16:03:06

+0

我認爲你必須要等到動畫完成集中輸入。嘗試增加'setTimeout'中的等待時間 – 2014-09-24 16:03:08

+0

嘗試在顯示模式之後獲取輸入 - jqueryui可能會複製元素。 – 2014-09-24 16:05:26

回答

1

使用顯示事件

$(function(){ 
    $input = $("#letter-change-text").find("input[type='text']"); 
    $input.val($.trim("I am just testing!!")); 
    $('#letter-change-text').on('shown.bs.modal', function (e) { 
     $input.focus(); 
    }).modal('show'); 
}); 
+0

那麼,我剛剛編輯我的帖子,解決方案..但我會再次編輯並接受你的答案。 – Linesofcode 2014-09-24 16:06:46

+0

爲什麼你要編輯一個帖子來獲得解決方案,這就是答案。 – epascarello 2014-09-24 16:07:32

相關問題