Javascript按键进入按键

时间:2016-12-11 22:10:30

标签: javascript jquery triggers keypress

我编写了一个函数addMoney(),它通过单击Enter到输入字段来调用:

$('body').on('keypress', 'input[type=text]', function(e){
     if(e.keyCode==13){
         var val = $('input[type=text]').val();
         switch(val) {
             case '-help': 
                 SeeComands();   
                 break
             case '-copyright':
                 SeeAuthors();
                 AddNewLine();
                 break
             case '-addmoney':
                 addMoney();
                 break
             default: 
                 SaveCommand();
                 Animal('I see nothing here, try smthing else',function(){AddNewLine();})
         } 
     }

此函数创建一个新的输入字段,单击Enter,在其中开始查找字符串中的正则表达式,该字符串已移交给上一个输入:

function addMoney() {
SaveCommand();

Animal('E', function(){//интсрукция ко вводу  Enter the number of banknotes introduced through the gap
    Animal('U',function(){ //Use a template <count of banknotes>x<nominal of banknotes>
        AddNewLine();
        $('body').on('keypress', 'input[type=text]', function(e){//Checking the input string
             if(e.keyCode==13){
                 var vali = $('input[type=text]').last().val();
                 console.log('val: ', vali);
                 SaveCommand();
                 var reg100 = /([1-9]{1,})(?=x100)/g; //RegExp to find the amount of the nominal value of 100 bills
                 var reg200 = /([1-9]{1,})(?=x200)/g; //RegExp to find the amount of the nominal value of 200 bills
                 var reg500 = /([1-9]{1,})(?=x500)/g; //RegExp to find the amount of the nominal value of 500 bills
                 var reg1000 = /([1-9]{1,})(?=x1000)/g; //RegExp to find the amount of the nominal value of 1000 bills
                 var reg5000 = /([1-9]{1,})(?=x5000)/g; //RegExp to find the amount of the nominal value of 5000 bills
                 var result100 = vali.match(reg100);
                 var result200 = vali.match(reg200);
                 var result500 = vali.match(reg500);
                 var result1000 = vali.match(reg1000);
                 var result5000 = vali.match(reg5000);
                 console.log('100:', result100, '200:', result200, '500:', result500, '1000:', result1000);
             };
        });
    });
});
};

但是开关的刹车工作提前了,直到AddMoney()工作。 结果vali在:

中声明
var vali = $('input[type=text]').last().val();

未定义,因此我没有addMoney()的结果,但有一个默认的开关结果: 动画“我在这里什么也看不见,试试别的东西” 我认为它可以b函数Animal():

function Animal(string, callback) {
    var a = ''; //The variable which will be entered character by character string
    var i= 0;//Counter of a letters
    var p = document.createElement('p');
    $('body').append(p);

    Anima();
    function Anima() {//Animation Function  
        a=a+string[i];
        i++;
        $('p').last().text(a);
        var timer = setTimeout(Anima, 100);
        if(i==string.length){
            clearTimeout(timer);
            callback();//For running in order
        };
    };
};

...到期函数Anima(),它通过SetTimeOut()

作为递归运行

1 个答案:

答案 0 :(得分:0)

我做了一个新功能DefaultCase():

function addMoney(str){
var reg100 = /([0-9]+)(?=x100)/g; //Регулярка для поиска количества купюр номинала 100
var reg200 = /([0-9]+)(?=x200)/g; //Регулярка для поиска количества купюр номинала 200
var reg500 = /([0-9]+)(?=x500$|x500\s)/g; //Регулярка для поиска количества купюр номинала 500
var reg1000 = /([0-9]+)(?=x1000)/g; //Регулярка для поиска количества купюр номинала 1000
var reg5000 = /([0-9]+)(?=x5000)/g; //Регулярка для поиска количества купюр номинала 5000
var result100 = str.match(reg100);
var result200 = str.match(reg200);
var result500 = str.match(reg500);
var result1000 = str.match(reg1000);
var result5000 = str.match(reg5000);

if((result100 == 0) && (result200 == 0) && (result500 == 0) && (result1000 == 0) && (result5000 == 0)){
    Animal('Enter the amount u need to add. Check the errors', function(){
        AddNewLine();
    });
} else {
    if ((valFree(100)<result100) || (valFree(200)<result200) || (valFree(500)<result500) || (valFree(1000)<result1000) || (valFree(5000)<result5000)) {
        Animal('U r tryig to add too many banknotes. Check free place in ATM', function(){
            AddNewLine();
        });
    } else {
        var resstring = 'success: '+str;
        Animal(resstring, function(){
            resstring = '100:+['+result100+']bills 200:+['+result200+']bills 500:+['+result500+']bills 1000:+['+result1000+']bills 5000:+'+result5000;
            Animal(resstring, function() {
                AddNewLine();
            })
        })};
    }
};

//Функция изменения имени
function ChangeName(str) {
if(str.indexOf('@')<(str.length-1)){
    name = str.match(/@(\w+)/)[1];//Регулярка поиска имени
    AddNewLine();
} else {
    Animal('Enter Valid Name.', function(){
        AddNewLine();
    }); 
};
};

//Функция Стандартного Default case в switch
function defaultCase(str) {
SaveCommand()
if(str.match(/-addmoney/)) {
    addMoney(str);
} else {
    if(str.match(/-name @/)){
        ChangeName(str);
    } else {
    Animal('I see nothing here, try smthing else',function(){AddNewLine();})

    }
}
};