【问题标题】:how to display stored data(from database) in textarea which is in json format如何在 json 格式的 textarea 中显示存储的数据(来自数据库)
【发布时间】:2015-12-18 23:20:18
【问题描述】:

这个解决方案不起作用displaying stored data in textarea.. 你能检查我的代码,让我知道有什么问题吗?

在 Ruby 中,我有一个处理程序,它将从数据库中获取加密数据并解码并发送到 jquery 函数,该函数将在下拉表更改时在 textarea 上显示来自数据库的文本

Ruby code:
    get '/show_previousdata' do
        projectName=params[:name]
        record=Array.new
        description=Array.new
        record=Project.filter(:project_name=>projectName).first
        project_id=record[:projects_id]
        description=Highlight.filter(:projects_id=>project_id).first
        if description==nil
           highlight="no data"
        else
           highlight=Zlib::Inflate.inflate(description[:description]) 
        end 
        highlight.to_json
   end
Jquery code:
   $("#project_list").change(function () {
     var index = $(this).find("option:selected").index();
     var text_val = $(this).find("option:selected").text();
     if(index!=0)
     {
        $.get('/show_previousdata',
        {
            name:text_val
        },
        function(data,status){
            $("#text_area").val($("<div/>").html(data).text());    
        });
        $('#highlights_textarea').show();
     }
     else
     {
        $('#highlights_textarea').hide();
     }
}); 
haml code:
 %textarea{:id => "text_area",:cols => "20", :rows => "20",:class => "textarea1"}

【问题讨论】:

  • 你能解释一下你所面临的问题吗?
  • 我无法以我在该链接中提到的正确格式显示文本(来自数据库)...(stackoverflow.com/questions/32471160/…)
  • 当你做一个 console.log(data) 你得到什么价值?
  • 我以这种格式获取数据“你好,这是sireesha\n 1.some information \n2.some information”
  • 你可以试试下面的答案看看

标签: jquery html ruby json postgresql


【解决方案1】:

你可以试试这个看看

$("#toTest").val($("<div/>").html("hello this is sireesha \r\n 1. hello \r\n 2. hello2 ").text());

小提琴

https://jsfiddle.net/en6f2mq8/3/

【讨论】:

  • 这并没有提供问题的答案。要批评或要求作者澄清,请在其帖子下方发表评论。
  • @silwar 明白了!会这样做
  • 我尝试使用它..但它在我的代码中不起作用..如果我在 javascript 中使用变量以字符串格式存储一些数据..然后如果我尝试显示它,那么它就是工作正常......但我的数据来自 json 格式的 ruby​​ 代码......可能是因为它不工作
  • 然后做 JSON.stringify(data) 并尝试
猜你喜欢
  • 2013-08-08
  • 2016-10-14
  • 2015-12-04
  • 2021-11-28
  • 1970-01-01
  • 2020-05-20
  • 2021-12-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多