gpt4 book ai didi

javascript - 将正文中的所有数字转换为阿拉伯数字,而不是选择列表 RoR 上的值

转载 作者:行者123 更新时间:2023-11-28 01:57:03 24 4
gpt4 key购买 nike

嗨,我一直在努力实现这一目标。需要将所有数字 1,2,3,4,5 (或 1956,1986 等)转换为阿拉伯数字 ١,٢,٣,٤,٥,٦,٧,٨,我在这样的测试中实现了这一点

我必须使用该值作为>1<,这样 value="1"就不会受到影响,因为在数据库中我需要将其存储为 1,而不是 ١

因此,在此之前,相同的代码仅使用 1 作为值,效果很好,将每个数字替换为其在阿拉伯语中的类似值,但也在 value=""中,所以这个螺丝所有的应用程序。因为在其他页面中,当我获取信息时,我得到的是 ١ 而不是 1

<script>
$(document).ready(function() {
$("select").html(String($('select').html()).replace(/>1</g, '>١<'));
$("select").html(String($('select').html()).replace(/>2</g, '>٢<'));
$("select").html(String($('select').html()).replace(/>3</g, '>٣<'));
$("select").html(String($('select').html()).replace(/>4</g, '>٤<'));
$("select").html(String($('select').html()).replace(/>5</g, '>٥<'));
$("select").html(String($('select').html()).replace(/>6</g, '>٦<'));
$("select").html(String($('select').html()).replace(/>7</g, '>٧<'));
$("select").html(String($('select').html()).replace(/>8</g, '>٨<'));
$("select").html(String($('select').html()).replace(/>9</g, '>٩<'));
});
</script>

有谁有想法,让这种情况发生在所有主体(不仅仅是选择)中,并且不改变输入、选择、检查或单选按钮上的 value=""数字,

谢谢!

好吧,只是为了澄清为什么我需要这样做......我使用 Rails

我也在使用 i18n,但我找不到“翻译”数字的方法

 <div id="altura">
<%= f.label :height %>
<%= f.select :height, Player::HEIGHT.collect {|h| [ t("generales.#{h}"), h ] } , :include_blank => true %> <%= f.select :height_measure, Player::HEIGHT_MEASURE.collect {|h| [ t("generales.#{h}"), h ] } %>
<%= f.select :inches, Player::INCH.collect {|h| [ t("generales.#{h}"), h ] }, {}, :style => (@player.inches.present? && @player.height_measure == 'pies' ? 'display: inline ' : 'display: none') %>
<%= f.label :inches, :id => 'inch_label', :style => (@player.inches.present? && @player.height_measure == 'pies' ? 'display: inline ' : 'display: none') %>
</div>

最佳答案

我真的不明白为什么你只想转换数字,你真的应该转换整个文本。但无论如何,只是为了好玩,这里有一个可以按照您的意愿执行的功能。

请注意,浏览器将识别阿拉伯数字序列,例如“21 23”,并让它们从右向左读取,即“23 21”,即使数字中的数字仍然从左读取到右(因为阿拉伯数字就是这样工作的——尽管它们的书写是从右到左,但它们的数字是从左到右读的,就像英语一样)。

function replaceDigits(id) {
var el, text;
var arabicCharCodes = {
'0': '1632',
'1': '1633',
'2': '1634',
'3': '1635',
'4': '1636',
'5': '1637',
'6': '1638',
'7': '1639',
'8': '1640',
'9': '1641'
}

// Do not process script elements, add others that have content but
// shouldn't be processed, e.g. maybe object
var elementsToIgnore = { script:'script'};

// If passed a string, expect ID so get element
el = typeof id == 'string'? document.getElementById(id) : id;

// If no element, return
if (!el) return;

var node, childNodes = el.childNodes;

for (var i=0, iLen=childNodes.length; i<iLen; i++) {
node = childNodes[i];

// Recurse over all nodes and only replace the content of text nodes
// in elements that are not in the ignore list
if (node.nodeType == 1 && node.tagName && !(node.tagName in elementsToIgnore)) {
replaceDigits(node);

} else if (node.nodeType == 3) {
text = node.data.split('');

for (var j=0, jLen=text.length; j<jLen; j++) {

if (text[j] in arabicCharCodes) {
text[j] = String.fromCharCode(arabicCharCodes[text[j]]);
}
}
node.data = text.join('');
}
}
}

关于javascript - 将正文中的所有数字转换为阿拉伯数字,而不是选择列表 RoR 上的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18969486/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com