2013-06-19 60 views
0

我有一個文本框,其中禁止輸入字符。 #。按下箭頭鍵時光標正在跳躍

但是,這適用於文本框填充數據時,我將焦點放在文本框的中間,然後使用箭頭鍵左右移動,然後跳轉到最後文本框。

如果我在文本框的中間還輸入一個字符,它去年底再次

$('[id$=txtClient]').keyup(function() { 
     EnableClientValidateButton(); // When the textbox changes, the user has the ability to validate the client 
     ChangeColorClient("0"); // The color is changed to white, to notify the user the client is not validated yet. 
     var $el = $('[id$=txtClient]'); // the text element to seach for forbidden characters. 
     var text = $el.val(); // The value of the textbox 
     text = text.split("#").join("");//remove occurances of forbidden characters, in this case # 
     $el.val(text);//set it back on the element 
    }); 
+0

你阻止他們進入#的方式是錯誤的。你應該檢查keyup並且不要在那裏允許#鍵。 – Archer

+0

我該怎麼做? –

+0

只是讓你演示,但克里斯托弗先到那裏:) – Archer

回答

1

這是一個有點不愉快,我不是100%滿意,但它解決了所有的問題,給你已經有...

$("[id$=txtClient]").keyup(function (e) { 
    var text = $(this).val(); 
    if (text.indexOf("#") > -1) { 
     text = text.replace("#", ""); 
     $(this).val(text); 
    } 
}); 

下面是一個例子的jsfiddle ...

http://jsfiddle.net/E4cBK/

0

您可以防止當用戶按下向右或向左箭頭執行代碼。 要做到這一點,你只需要添加此條件:

if(e.which != 37 && e.which != 39){ 

您可以找到關鍵代碼here

你完整的代碼是:

$('[id$=txtClient]').keyup(function() { 
    if(e.which != 37 && e.which != 39){ 
     EnableClientValidateButton(); 
     ChangeColorClient("0"); 
     var $el = $('[id$=txtClient]'); 
     var text = $el.val(); 
     text = text.split("#").join(""); 
     $el.val(text);//set it back on the element 
    } 
}); 

LIVING EXAMPLE

+1

只是爲了防止#鍵按下不是更容易嗎?用你的例子,你還沒有解決在文本框中間輸入的問題。 – Archer

+0

@Archer你是對的。我忘了那部分......我會再看一次。 – Alvaro

0

只是防止默認操作上按鍵(的keydown沒有給出一致charCodes):

$('[id$=txtClient]').keypress(function (e) { 
     if (String.fromCharCode(e.which) == '#'){ 
      e.preventDefault(); 
     } 
}); 

這只是防止#並保持原樣。

Here you go;)

+0

它不適合我,你仍然可以介紹'#'... – Alvaro

+0

試過演示?哪個瀏覽器? – Christoph

+0

我已經改變了你的字符代碼 - 應該是222 – Archer

3

JavaScript中允許你設置輸入光標位置。

我發現了兩個有用的功能:

而且該解決方案看起來是這樣的:

function getCaretPosition (elem) { 

    // Initialize 
    var iCaretPos = 0; 

    // IE Support 
    if (document.selection) { 

     // Set focus on the element 
     elem.focus(); 

     // To get cursor position, get empty selection range 
     var oSel = document.selection.createRange(); 

     // Move selection start to 0 position 
     oSel.moveStart ('character', -elem.value.length); 

     // The caret position is selection length 
     iCaretPos = oSel.text.length; 
    } 
    // Firefox support 
    else if (elem.selectionStart || elem.selectionStart == '0') 
     iCaretPos = elem.selectionStart; 

    // Return results 
    return (iCaretPos); 
    } 

    function setCaretPosition(elem, caretPos) { 
     if(elem != null) { 
      if(elem.createTextRange) { 
       var range = elem.createTextRange(); 
       range.move('character', caretPos); 
       range.select(); 
      } 
      else { 
       if(elem.selectionStart) { 
        elem.focus(); 
        elem.setSelectionRange(caretPos, caretPos); 
       } 
       else 
        elem.focus(); 
      } 
     } 
    } 

$('[id$=txtClient]').keyup(function() { 
    EnableClientValidateButton(); // When the textbox changes, the user has the ability to validate the client 
    ChangeColorClient("0"); // The color is changed to white, to notify the user the client is not validated yet. 
    var $el = $('[id$=txtClient]'); // the text element to seach for forbidden characters. 
    var text = $el.val(); // The value of the textbox 
    text = text.split("#").join("");//remove occurances of forbidden characters, in this case # 

    var pos = getCaretPosition(this); 
    $el.val(text);//set it back on the element 
    setCaretPosition(this, pos); 
}); 
+0

謝謝! (注意:也適用於Chrome和Opera) – T4NK3R

1

您是否嘗試過使用keypress事件?

documentation警告平臺之間的行爲可能存在差異。

在Firefox至少,e.which對應於輸入的字符的ASCII碼改造後:

$('#txtClient').keypress(function (e) { 
    console.log('keypress:', e.which); 
    if (e.which == 35) { 
     return false; 
    } 
}); 

updated fiddle

+0

不幸的是,當試圖在中間插入'#'時,光標依然在跳躍。我想你不能很容易地解決這個問題,因爲幽閉恐懼症提出的答案是好的,但是CaretPosition不被所有的瀏覽器支持。 – Christoph