Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/439.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将正文中的所有数字转换为阿拉伯数字,而不是选择列表中的值_Javascript_Jquery_Ruby On Rails_Ruby On Rails 3 - Fatal编程技术网

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

Javascript 将正文中的所有数字转换为阿拉伯数字,而不是选择列表中的值,javascript,jquery,ruby-on-rails,ruby-on-rails-3,Javascript,Jquery,Ruby On Rails,Ruby On Rails 3,嗨,我一直在努力做到这一点。需要将所有数字1,2,3,4,5(或1956年1986年等)转换为阿拉伯数字١،،٣،٤،٦،،،،٩我在这样的测试中实现了这一点 我必须使用>1的值,这样值=“1”不会受到影响,因为在db中,我需要将其存储为1,而不是١ 因此,在此之前,相同的代码仅使用1作为值,效果很好,替换everynumber,因为它在阿拉伯语中类似,但在value=”“中也一样,所以这会影响所有应用程序。因为在其他页面,当我得到信息时,我得到的是١而不是1 <script> $(

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

我必须使用>1的值,这样值=“1”不会受到影响,因为在db中,我需要将其存储为1,而不是١

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

$(文档).ready(函数(){
$('select').html(字符串($('select').html()).replace(/>123456789
(@player.inches.present?&&&@player.height_measure=='pies'?'display:inline':'display:none')%>
'inch_label',:style=>(@player.inches.present?&&&&@player.height_measure='pies'?'display:inline':'display:none')%>

我真的不明白为什么你只想转换数字,你真的应该转换整个文本。但无论如何,为了让它见鬼,这里有一个函数可以按照你的要求执行

请注意浏览器将识别阿拉伯数字序列,如“21 23”,并使其从右向左读取,即“23 21”,即使数字中的数字仍然从左向右读取(因为阿拉伯数字的书写方式是从右向左,数字从左向右读取,就像英语一样,这就是阿拉伯数字的工作方式)

函数替换数字(id){
变量el,文本;
变量arabicCharCodes={
'0': '1632',
'1': '1633',
'2': '1634',
'3': '1635',
'4': '1636',
'5': '1637',
'6': '1638',
'7': '1639',
'8': '1640',
'9': '1641'
}
//不要处理脚本元素,添加其他包含内容但
//不应处理,例如,可能是对象
var elementsToIgnore={script:'script'};
//如果传递了一个字符串,则应为ID so get元素
el=typeof id=='string'?document.getElementById(id):id;
//如果没有元素,则返回
如果(!el)返回;
var节点,childNodes=el.childNodes;

对于(var i=0,iLen=childNodes.length;i我真的不明白为什么您只想转换数字,您真的应该转换整个文本。但是,不管怎样,这里有一个函数可以按照您的需要执行

请注意浏览器将识别阿拉伯数字序列,如“21 23”,并使其从右向左读取,即“23 21”,即使数字中的数字仍然从左向右读取(因为阿拉伯数字的书写方式是从右向左,数字从左向右读取,就像英语一样,这就是阿拉伯数字的工作方式)

函数替换数字(id){
变量el,文本;
变量arabicCharCodes={
'0': '1632',
'1': '1633',
'2': '1634',
'3': '1635',
'4': '1636',
'5': '1637',
'6': '1638',
'7': '1639',
'8': '1640',
'9': '1641'
}
//不要处理脚本元素,添加其他包含内容但
//不应处理,例如,可能是对象
var elementsToIgnore={script:'script'};
//如果传递了一个字符串,则应为ID so get元素
el=typeof id=='string'?document.getElementById(id):id;
//如果没有元素,则返回
如果(!el)返回;
var节点,childNodes=el.childNodes;

对于(var i=0,iLen=childNodes.length;我觉得这是一种可怕的方法。很明显,这些数字是某些标记的内容。有趣的是,你决定不显示标记的外观。我绝不会这样做。同意@JonathanWood的说法,肯定有比这更好的方法。你为什么在客户端上这样做?发送从服务器获取阿拉伯文字体的数据,然后将选项元素的值设置为适当的英文字符。这样,将显示阿拉伯文数字,但英文字符将返回到服务器。并在服务器上运行替换(换句话说:属性都保持原样).Im on Rails我会粘贴标签。现在你可以看到一个HTML标签的例子,Im on Rails,如果你能想出更好的方法,请尽管做我的客人。另外请注意,这个页面有10个字节。这就是为什么9个字节使用普通数字,这就是为什么我使用普通数字在数据库中存储数字。谢谢。如果值为1,Rails也会发送错误转换为阿拉伯数字对我来说是一种可怕的方法。很明显,这些数字是某些标记的内容。有趣的是,你决定不显示标记的外观。我绝不会这样做。同意@JonathanWood,肯定有比这更好的方法。你为什么在客户端上这样做?发送数据使用来自服务器的阿拉伯语字体,然后将选项元素的值设置为适当的英语字符。这样,将显示阿拉伯语数字,但英语字符将返回到服务器。并在该服务器上运行替换(换句话说:属性保持原样).Im on Rails我会粘贴标签。现在你可以看到一个HTML标签的例子,Im on Rails,如果你能想出更好的方法,请尽管做我的客人。另外请注意,这个页面有10个字节。这就是为什么9个字节使用普通数字,这就是为什么我使用普通数字在数据库中存储数字。谢谢。如果值为1,Rails也会发送错误转换为阿拉伯语数字所有文本其已使用yml lenguage文件和gem i18n进行翻译,谢谢我会检查并更新,啊,因此您可能正在使用数据库提取一个只想更新选项元素文本?在这种情况下,只需向上面提供一个选择元素引用或其ID,它就会完成此工作,不会影响value属性选项(希望您没有使用文本作为值)。我一直在尝试翻译数字,我能够翻译国家,使用id,以及诸如此类的东西,但是数字,这是一个大问题,因为我无法翻译:(,我将更新问题你能让我知道为什么吗
 <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>
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('');
    }
  }
}