Javascript for replacing an abreviation by his equivalent

前端 未结 5 884
情话喂你
情话喂你 2021-01-26 05:00

I met some trouble with a javascript.

In fact I have in my database many records that are abreviations and ther equivalent,

for example, replace tel => telephon

5条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-26 05:42

    When using keypress this way the code variable will contain the character code of the pressed character. Not the string of chars like the expected 'tel'. You could use onkeyup / onchange event and check the val() of the input element and use replace() to change the abbreviation to the intended string.

    $('#tags').keyup(function(e){
    
        var elem = $(this);
            var input = elem.val();
    
            // input = input.substr(0, input.length -1);  // This might not be necessary
            console.log(input);
    
            // do the replacement
            input = input.replace('tel','telephone');
    
            elem.val(input);        
        }
    
    });
    

提交回复
热议问题