jQuery输入数字将波斯/阿拉伯数字转换为英文数字 [英] jQuery input number convert Persian/Arabic numbers to English numbers
问题描述
我有一个数字类型的输入字段.我也想在此字段中接受波斯/阿拉伯数字.我想使用jQuery将这些数字转换为英文,但看来我无法检索这些值.
I have a input field of type number. I want to accept Persian/Arabic numbers as well in this field. I wanted to convert these numbers to English using jQuery, but it seems I can't retrieve these values.
jQuery('input[type="number"]').on('input', function() {
console.log(jQuery(this).val());
});
对于非英文数字,val()
为空字符串.如果该值不是英文数字,有什么方法可以获取数字输入字段的值?
For non-English numbers val()
is empty string. Is there any way to get the value of a number input field, if the value is not a English number?
更新:
波斯数字是: 英文数字为:0 1 2 3 4 5 6 7 8 9
Persian numbers are: ۰ ۱ ۲ ۳ ۴ ۵ ۶ ۷ ۸ ۹ English numbers are: 0 1 2 3 4 5 6 7 8 9
UPDATE2:
貌似Firefox返回正确的val
,但Chrome返回空字符串.所以我的问题适用于Google Chrome.
Looks like Firefox returns the correct val
but Chrome returns empty string. So my question is applicable on Google Chrome.
推荐答案
您可以使用以下功能:
function toEnglishNumber(strNum) {
var pn = ["۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹"];
var en = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
var an = ["٠", "١", "٢", "٣", "٤", "٥", "٦", "٧", "٨", "٩"];
var cache = strNum;
for (var i = 0; i < 10; i++) {
var regex_fa = new RegExp(pn[i], 'g');
var regex_ar = new RegExp(an[i], 'g');
cache = cache.replace(regex_fa, en[i]);
cache = cache.replace(regex_ar, en[i]);
}
return cache;
}
这篇关于jQuery输入数字将波斯/阿拉伯数字转换为英文数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!