我的目标是将 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/