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
    });
4

5 に答える 5

1

keypressイベントを使ってみましたか?

ドキュメントは、プラットフォーム間の動作の違いの可能性について警告しています。

少なくとも Firefox では、e.which変換後の入力文字の ASCII コードに対応します。

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

更新されたフィドル

于 2013-06-19T10:12:58.837 に答える
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/

于 2013-06-19T09:42:47.983 に答える
0

キーを押したときのデフォルトのアクションを防ぐだけです(キーダウンは一貫したcharCodesを提供しません):

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

これ#は、残りをそのままにしておきます。

どうぞ;)

于 2013-06-19T09:11:05.660 に答える
0

ユーザーが右矢印または左矢印を押した場合に、コードが実行されないようにすることができます。これを行うには、次の条件を追加するだけです。

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

キーコードはこちらで確認できます。

完全なコードは次のようになります。

$('[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
    }
});

生きた例

于 2013-06-19T09:05:22.533 に答える