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

如何在提交时调用javascript函数为输入赋值?(ACE编辑),javascript,html,forms,submit,ace-editor,Javascript,Html,Forms,Submit,Ace Editor,我的目标是将ACE编辑器中的代码作为变量发送到send.phpphp文件。我尝试了许多不同的方法,但我无法将编辑器中的代码分配给表单输入 JavaScript 此javascript函数应使用id=“code”为元素分配一个值,即: editor.getSession().getValue()返回编辑器中的代码 <head> <script> function getVal() { document.getElementById('code').valu

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


JavaScript

此javascript函数应使用
id=“code”
为元素分配一个值,即:

editor.getSession().getValue()返回编辑器中的代码

<head>
 <script>

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

 </script>
</head>

代码是正确的,editor.getSession().getValue()为空,因此它不写任何内容

我需要这个函数来实现这一点:

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;
}

尝试在submit按钮中提供函数,如onclick=“return getVal();”。还要检查隐藏字段是否有正确的值,然后只重新运行true,否则返回false。我这样做了,它仍然不起作用。我将值添加到输入
,并检查它在js函数中是否有此值-它返回false。可能问题出在
文档.getElementById('code').value
?您能不能重命名文本框id并为其命名,因为您需要在php中获得正确的值?这没有问题文档。getElementById('code')。value@JobinJose什么文本框?它不是空的,因为它获取编辑器中的所有代码(在用户编写一些代码之后)。您是否尝试将其值打印到警报或console.log?在我尝试对文本进行隐藏输入,而不是editor.getSession().getValue()之前;我写了“测试”;当我提交表单时,它将测试写入输入,然后提交,所以问题一定是editor.getSession().getValue()<代码>文档.getElementById('code')。值='test'
你是对的,我试图通过执行
if(document.getElementById('code')和
来检查
document.getElementById('code')是否正常工作,但由于某种原因,它返回了false,所以我认为这就是问题所在。
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;
}