0

我有一个仅限输入数字的文本区域。我想在用户按下回车键后删除回车符。这是我的代码:

// Change tempo
function changeTempo(event:KeyboardEvent):void {
    if (event.charCode == 13) {
        // Some code here
    }
    // Remove enter char
    removeCarriageReturnsAndNewLines(tempo_txt.text);
}

function removeCarriageReturnsAndNewLines($myString:String):String {
    var newString:String;
    var findCarriageReturnRegExp:RegExp = new RegExp("\r", "gi");
    newString = $myString.replace(findCarriageReturnRegExp, "");
    var findNewLineRegExp:RegExp = new RegExp("\n", "gi");
    newString = newString.replace(findNewLineRegExp, "");
    return newString;
}
4

1 回答 1

1

我想说最简单的方法是听文本输入,如下所示:

var t:TextArea = this.ta; //ta is on the timeline
t.restrict = "0-9"; //restricts the input only to numbers
t.addEventListener(TextEvent.TEXT_INPUT, onTextInput, true); //use capture phase to be able to prevent the default behavior (text input)

function onTextInput(e:TextEvent):void {
    if(e.text.indexOf("\n") > -1) {
        e.preventDefault(); //prevent the default behavior of the field
    }
}

我现在无法对此进行测试,但我想它应该可以正常工作。

于 2015-01-19T23:51:19.600 回答