synthax highlighter - 编辑textarea元素中突出显示的文本

时间:2012-10-14 04:00:51

标签: jquery textarea syntaxhighlighter

我已经实现了Alex Gorbatchev的Synthax荧光笔,并且它工作得很好,但是当用户双击突出显示的文本(然后成为textarea的div)时,它进入编辑模式(在其中生成无名文本区域,其中包含高位文本数据)。我创建了一个按钮,并执行点击事件尝试从中获取新输入/编辑的文本以通过ajax重新发布它,但我不断获得textarea中的旧文本(变成一个高级div)或者#39 ;未定义&#39 ;.我使用jQuery来获取新输入的文本,但没有运气。非常感谢任何帮助!

extends layout
block content
  h2=d.title
    a(class='button', href='/documents/' + d._id + '/edit') Edit
  p
    a(href='/documents', class='button')
      ← All Documents
  script(type="text/javascript")
    SyntaxHighlighter.all();
    SyntaxHighlighter.config.tagName = "textarea";

  form(name="form1") 
    div(style="width:800px; margin:0 auto; border: 2px grey solid;")  
      textarea(class="brush: js;", style="width:800px;", readonly="true", id = "srcText", disabled)
        =d.data || ''

  button(id="submit", class='button')

  script
    $("#submit").click(function (e) {
      console.log("boom");
      var dId = '#{d._id}';
      var dData = $("textarea").val(); // << -- problem here cant obtain the value
      console.log("DATA  is: ", dData);
      var updateDocumentIdPath = "/documents/" + dId + "/update";
      $.ajax({
        url: updateDocumentIdPath, 
        type: "POST",  
        data: {"data": dData},
        success: function (data) {
          console.log("success");
          console.log(data);
        },
        error: function () {
          console.log("error");
        },
        complete: function () {
        }
      }); // END ajax
    }); 

1 个答案:

答案 0 :(得分:0)

要获取数据,您使用的是$("textarea").val();,它会在textarea中返回 first document的值。文档:http://api.jquery.com/val/

您需要的是:$("textarea:last").val();。这将返回textarea中最后document的值。假设它是textarea中的最后一个document,因为你提到它在切换到编辑模式之前被动态插入。

相关问题