我有3個電話號碼文本框。當用戶鍵入時,它會自動從一個文本框移動到另一個文本框。當用戶按退格鍵時,我可以將焦點移至上一個文本框。問題是,在IE中,焦點設置在文本框的開頭。這是我的代碼,可以在Chrome中正常工作。設置文本框中最後一個字符後的焦點
$('#AreaCode').live('keyup', function (event) {
if ($(this).val().length == $(this).attr("maxlength"))
$('#Prefix').focus();
});
$('#Prefix').live('keyup', function (event) {
if (event.keyCode == 8 && $(this).val().length == 0)
$('#AreaCode').focus();
if ($(this).val().length == $(this).attr("maxlength"))
$('#Number').focus();
});
$('#Number').live('keyup', function (event) {
if (event.keyCode == 8 && $(this).val().length == 0)
$('#Prefix').focus();
});
如何在倒退時在內容末尾設置焦點?
的重複http://stackoverflow.com/questions/511088/use-javascript-to-place-cursor-at-end-of-text-in-text-input-element – 2011-01-05 21:40:12
另一個簡單的方法在這裏:http://stackoverflow.com/a/19568146/1032372 – shim 2014-11-25 21:29:37