doukengzi3517 2016-03-31 08:53
浏览 56
已采纳

写入时更改输入字段值

Let's say I have a input field

<input type="text" class="form-control" name="filename">

and do write something like this:

Hällo

Is it possible to check while writing if that field has a the letter ä and change it to an a while writing ?

So far, I built this:

$search  = array('ä', 'Ä');
$replace = array('ae', 'Ae');

$project = str_replace($search, $replace, $input);
  • 写回答

2条回答 默认 最新

  • douruobokui58233 2016-03-31 09:01
    关注

    You don't do this with PHP, you would do it with Javascript:

    var el = document.getElementById("filename");
    el.addEventListener('keyup', function(){
        var replace = ['ä','å'];
        var replacewith = 'a';
        var replace1 = ['ö'];
        var replacewith1 = 'o';
        replace.forEach(function(letter){
            el.value = el.value.replace(letter, replacewith);
        });
        replace1.forEach(function(letter){
            el.value = el.value.replace(letter, replacewith1);
        });
    });
    

    Add id="filename" to the input element for this to work. You can add as many letters to the replace array as you would like. You can also add more arrays to replace letters.

    https://jsfiddle.net/dek5as1x/1

    Edit: Solution for several letters

    var el = document.getElementById("filename");
    el.addEventListener('keyup', function(){
        var replaces = [['ä','å'],['ö','ø'],['ï','ì'],['ü','ù','û'],['ë','ê']];
        var replacewith = ['a','o','i','u','e'];
        replaces.forEach(function(letterGroup, index){
            letterGroup.forEach(function(letter){
                el.value = el.value.replace(letter, replacewith[index]);
            });
        });
    });
    

    Here you add a new array ([]) to replaces. Then you add all the letters to that array that should be turned into the same letter to that array. Example: ['ê','ë','è']. Then you add the letter to the replacewith array. It is important that the letter comes to replace the letters in the array has the same index as the corresponding array in replaces. This solution should be a little cleaner for when you have lots of letters to replace.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路