Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 更改失败时的CodeMirror_Javascript_Codemirror - Fatal编程技术网

Javascript 更改失败时的CodeMirror

Javascript 更改失败时的CodeMirror,javascript,codemirror,Javascript,Codemirror,我正在从表单中的文本区域创建两个代码镜像实例,我需要在提交之前更新这些隐藏的文本区域。我在脚本中添加了更改事件,但它似乎不起作用 有人能帮忙吗? 谢谢 函数编辑器(id){ 变量编辑器=CodeMirror.fromTextArea(id{ 连续扫描:500, 行号:对 }); 编辑设置大小(900600); } var config\u id=document.getElementById('id\u config')) 变量配置=编辑器(配置id); var remote\u config

我正在从表单中的文本区域创建两个代码镜像实例,我需要在提交之前更新这些隐藏的文本区域。我在脚本中添加了更改事件,但它似乎不起作用

有人能帮忙吗? 谢谢


函数编辑器(id){
变量编辑器=CodeMirror.fromTextArea(id{
连续扫描:500,
行号:对
});
编辑设置大小(900600);
}
var config\u id=document.getElementById('id\u config'))
变量配置=编辑器(配置id);
var remote\u config\u id=document.getElementById('id\u remote\u config'))
var remote\u config=editor(remote\u config\u id);
onconfig.on('change',函数(cMirror){
//从实例中正确获取值
config_id.value=cMirror.getValue();
});
远程配置打开('change',函数(cMirror){
//从实例中正确获取值
远程_config_id.value=cMirror.getValue();
});

您不能使用
change
事件:CodeMirror侦听隐藏文本区域的更改,因此更改值将触发另一个
change
事件。这可能会导致无休止的循环

文档包含正确的方法:

该库提供了更强大的快捷方式:

var mycodeirror=CodeMirror.fromTextArea(myTextArea)

这将确保在提交表单(如果它是表单的一部分)时,使用编辑器的内容更新textarea的值

这意味着您的代码中有一个上面没有显示的bug。也许您使用了一种奇怪的方式提交表单,所以CodeMirror无法注意到并更新该值


一个选项是删除提交表单的JavaScript中的CodeMirror。

您不能使用
change
事件:CodeMirror侦听隐藏文本区域的更改,因此更改值将触发另一个
change
事件。这可能会导致无休止的循环

文档包含正确的方法:

该库提供了更强大的快捷方式:

var mycodeirror=CodeMirror.fromTextArea(myTextArea)

这将确保在提交表单(如果它是表单的一部分)时,使用编辑器的内容更新textarea的值

这意味着您的代码中有一个上面没有显示的bug。也许您使用了一种奇怪的方式提交表单,所以CodeMirror无法注意到并更新该值


一个选择是删除提交表单的JavaScript中的CodeMirror。

hmm,看看这个,我认为可能是我的服务器端代码不起作用。当我提交表单时,更新仍然存在,页面重新加载,但是当我刷新页面时,旧内容又回来了。嗯,仔细看,我认为可能是我的服务器端代码不起作用。当我提交表单时,更新会保留,页面会重新加载,但是当我刷新页面时,旧内容会返回
<script type="text/javascript"> 
    function editor(id) {
            var editor = CodeMirror.fromTextArea(id, {
                continuousScanning: 500,
                lineNumbers: true
            });
            editor.setSize(900, 600);
        }
    var config_id = document.getElementById('id_config')
    var config = editor(config_id);
    var remote_config_id = document.getElementById('id_remote_config')
    var remote_config = editor(remote_config_id);

    config.on('change',function(cMirror){
        // get value right from instance
        config_id.value = cMirror.getValue();
    });
    remote_config.on('change',function(cMirror){
        // get value right from instance
        remote_config_id.value = cMirror.getValue();
    });
</script>