1

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

我必须将值用作 >1<,因此 value="1" 不会受到影响,因为在 db 中我需要将其存储为 1,而不是 ١

因此,在此之前,仅使用 1 作为值的相同代码效果很好,将everynumber 替换为它在 arabic 中的相似值,但也在 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>
4

1 回答 1

1

I really don't understand why you just want to convert the digits, you really should be converting the entire text. But anyway, just for the heck of it, here's a function that does as you want.

Be aware that browsers will recognise sequences of Arabic numbers such as "21 23" and make them read right to left, i.e. "23 21" even though the digits in the number still read left to right (because that's how Arabic numbers work—even though their writing is right to left, their numbers read left to right, just like English).

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('');
    }
  }
}
于 2013-09-24T00:58:04.423 回答