Javascript 输入时转换输入语言
我有一个输入字段,可以接受阿拉伯语和英语两种语言,我想要的是,当我在这个输入中键入阿拉伯语数字以在用户键入时将其转换为英语时,我希望它们从['1776;','1777;','1778;','1779;','1780;','1781;','1783;','1784;','1785;]转换为['0','1','2','3','4','5','6','7','8','9'],我该怎么做?这是我的输入代码: 对输入使用oninput处理程序来监视文本字段中的更新。然后对输入元素的值使用Stringreplace将所有数字替换为所需的数字 replace方法可以使用一个函数,我在下面的代码中使用了这个函数,通过它在map数组中的索引将数字直接映射到阿拉伯语表示 设map=[‘۰’、‘۱’、‘۳’、‘۴’、‘۵’、‘۶’、‘۸’、‘۹’]; 函数变换器{ el.value=el.value.replace/\d/g,函数匹配{ 返回地图[匹配] } } 对输入使用oninput处理程序来监视文本字段中的更新。然后对输入元素的值使用Stringreplace将所有数字替换为所需的数字 replace方法可以使用一个函数,我在下面的代码中使用了这个函数,通过它在map数组中的索引将数字直接映射到阿拉伯语表示 设map=[‘۰’、‘۱’、‘۳’、‘۴’、‘۵’、‘۶’、‘۸’、‘۹’]; 函数变换器{ el.value=el.value.replace/\d/g,函数匹配{ 返回地图[匹配] } } 您可以使用需要为您的应用程序指定的onkeypress属性Javascript 输入时转换输入语言,javascript,jquery,html,Javascript,Jquery,Html,我有一个输入字段,可以接受阿拉伯语和英语两种语言,我想要的是,当我在这个输入中键入阿拉伯语数字以在用户键入时将其转换为英语时,我希望它们从['1776;','1777;','1778;','1779;','1780;','1781;','1783;','1784;','1785;]转换为['0','1','2','3','4','5','6','7','8','9'],我该怎么做?这是我的输入代码: 对输入使用oninput处理程序来监视文本字段中的更新。然后对输入元素的值使用Stringre
<input type="text" placeholder="Numbers should be converted from Arabic to English here" onkeypress = "convertArabicToNumbers(this)">
您可以使用需要为您的应用程序指定的onkeypress属性
<input type="text" placeholder="Numbers should be converted from Arabic to English here" onkeypress = "convertArabicToNumbers(this)">
试试这个,我正在做英语到阿拉伯语的btw,你应该改变数组 输入
<input id="inputTest" type="text" placeholder="Numbers should be converted from Arabic to English here">
希望有帮助,试试这个,我正在做英语到阿拉伯语的练习,顺便说一句,你应该更改数组 输入
<input id="inputTest" type="text" placeholder="Numbers should be converted from Arabic to English here">
希望有帮助,对于阿拉伯语和波斯语字符:
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]
})
}
对于阿拉伯语和波斯语字符:
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]
})
}
对于所有具有文本类型的输入 使用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);
}
});
对于所有具有文本类型的输入 使用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);
}
});
我想他的意思是相反的。从阿拉伯语到英语。你是对的。我添加了一个编辑,但两种方式都保留了。我想他的意思是相反的。从阿拉伯语到英语。你是对的。我添加了一个编辑,但保留了两种方式。