javascript - 如何在提交时调用 javascript 函数来为输入赋值? (ACE编辑)

标签 javascript html forms submit ace-editor

我的目标是将 ACE 编辑器内的代码作为变量发送到 send.php PHP 文件。我尝试了许多不同的方法,但无法将编辑器内的代码分配给表单输入。

<小时/>

JavaScript

此 javascript 函数应为带有 id="code" 的元素分配一个值。 ,即:<input type="hidden" id="code" value="" /> .

editor.getSession().getValue();返回编辑器内的代码。

<head>
 <script>

   function getVal() {
   document.getElementById('code').value = editor.getSession().getValue();
   }

 </script>
</head>

HTML

现在,<form onsubmit="getVal();"应该执行function getVal()当用户提交表单时,code将输入发送到 send.php 时,输入将具有一个值文件。

<body>

  <div>
     <form onsubmit="getVal();" method="post" action="send.php">

        <label for="Name">From:</label>
        <input type="text" name="Name" id="Name" />
        <label for="Address">To:</label>
        <input type="text" name="Address" id="Address" />
        <label for="Subject">Subject:</label>
        <input type="text" name="Subject" id="Subject" />

        <input type="hidden" id="code" value="" />

        <div id="editor"> //ACE Editor
        </div>

        <input type="submit">

     </form>
  </div>

最佳答案

为了完成这项工作,我需要这个函数:

function getVal()
{
  var editor = ace.edit("editor"); 
  // this line is necessary
  // "editor" is the id of the ACE editor div

  var code = editor.getSession().getValue();

  document.getElementById('code').value = code;
}

关于javascript - 如何在提交时调用 javascript 函数来为输入赋值? (ACE编辑),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12674023/

相关文章:

php - 将页眉和页脚作为模板包含在内不好吗?

html - 使最后一个菜单项背景颜色不同,以扩展并填充其余空间?

javascript - jQuery Validation 插件远程规则更改 php 函数值

javascript - RequireJS 插件( order.js )

javascript - 调整大小的夜巡窗口更小

javascript - 使用 Ajax/jQuery 同时发布两个表单

VB.NET:如何在其他子循环期间保持主窗体控制?

JavaScript:获取编辑/更新输入的数量

c# - Microsoft 表单预览 API

javascript - 为什么重新加载有一半时间返回空状态?