键入

时间:2017-07-12 11:14:13

标签: javascript jquery html

我有一个输入字段,接受两种语言阿拉伯语和英语,我想要的是当我在输入中键入阿拉伯数字以在用户输入时将它们转换为英语时,我希望它们从{{1}转换} ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'],我怎么能这样做?这是我的输入代码:



['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']




5 个答案:

答案 0 :(得分:3)

使用输入上的oninput处理程序在文本字段中查看更新。然后在输入元素的值上使用String#replace将所有数字替换为您需要的数字。

replace方法可以使用一个函数,我使用了该函数在下面的代码中,通过map数组中的索引将数字直接映射到阿拉伯语表示。



let map =  ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];

function change(el){
  el.value = el.value.replace(/\d/g, function(match){
    return map[match]
  })
}

<input type="text" placeholder="Numbers should be converted from Arabic to English here" oninput="change(this)">
&#13;
&#13;
&#13;

<强> **编辑**

我绊倒了。 OP希望从阿拉伯语到英语。 只需将地图更改为将阿拉伯语表示映射到英语的对象,然后将替换中的正则表达式更改为捕获阿拉伯语而不是数字。

&#13;
&#13;
let map =  {
  '۰' : "0", 
  '۱' : "1", 
  '۲' : "2", 
  '۳' : "3", 
  '۴' : "4",
  '۵' : "5", 
  '۶' : "6", 
  '۷' : "7",
  '۸' : "8", 
  '۹' : "9"
 };

function change(el){
  el.value = el.value.replace(/[۰۱۲۳۴۵۶۷۸۹]/g, function(match){
    return map[match]
  })
}
&#13;
<input type="text" placeholder="Numbers should be converted from Arabic to English here" oninput="change(this)">
&#13;
&#13;
&#13;

答案 1 :(得分:1)

试试这个,(我正在用英语做阿拉伯语btw,你应该改变数组)

输入,

<input id="inputTest" type="text" placeholder="Numbers should be converted from Arabic to English here">

的Javascript,

var arabics = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];
var engs = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];

$("#inputTest").on("keyup", function(e) {
  var insideText = String.fromCharCode(e.which);
  var index = engs.indexOf(insideText);
  if (index >= 0) {
    var value = $("#inputTest").val();
    value = value.replace(insideText, arabics[index]);
    $("#inputTest").val(value);
  }

});

demo

希望有所帮助,

答案 2 :(得分:0)

您可以使用需要提供给您的onkeypress属性。

<input type="text" placeholder="Numbers should be converted from Arabic to English here" onkeypress = "convertArabicToNumbers(this)">

这个属性将包含一个javascript函数名,这个函数的实现如下,

让我们说javascript函数是

function convertArabicToNumbers(element){
var value = element.value;
// logic to read each character from value variable and and compare it with arabic array and replace each character with number array
}

答案 3 :(得分:0)

对于阿拉伯语和波斯语字符:

 let map = {
    "۱": "1",
    "۲": "2",
    "۳": "3",
    "۴": "4",
    "۵": "5",
    "۶": "6",
    "۷": "7",
    "۸": "8",
    "۹": "9",
    "۰": "0",
    "١": "1",
    "٢": "2",
    "٣": "3",
    "٤": "4",
    "٥": "5",
    "٦": "6",
    "٧": "7",
    "٨": "8",
    "٩": "9",
    "٠": "0"
};

function changeToEngNum(el){
    el.value = el.value.replace(/[۰۱۲۳۴۵۶۷۸۹١٢٣٤٥٦٧٨٩٠]/g, function(match){
        return map[match]
    })
}

答案 4 :(得分:0)

对于所有具有文本类型的输入 使用jQuery库

var arabics = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];
var engs = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];


$(document).on('keyup', 'input[type=text]', function (e) {

    var insideText = (e.key);
    var index = arabics.indexOf(insideText);
    if (index >= 0) {
        var value = $(this).val();
        value.substr(0, value.length - 1) != null ? value = value.substr(0, value.length - 1) + engs[index] : value = engs[index];
        $(this).val(value);
    }


});
相关问题