如何在按下后删除回车符?

时间:2015-01-19 21:49:38

标签: actionscript-3 flash-cc

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

// 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;
}

1 个答案:

答案 0 :(得分: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
    }
}

我现在无法对此进行测试,但我想它应该没有问题。