表单被替换为文本区域,不会给出不同的输出


form replaced to textarea not give different output

我有两个文件

  1. index.php
  2. 代码.js

我在index.php中有一些代码,其中包含表单,并在code.js的帮助下。我正在用该表单替换pre-tag。所以一切都很好。为了理解我的问题,你必须查看我的代码。所以首先我给我的代码

index.php文件:

<html>
<head>
        <link rel="stylesheet" href="http://code.guru99.com/css/1140.css" type="text/css" media="screen" />
        <link rel="stylesheet" href="http://code.guru99.com/css/styles.css" type="text/css" media="screen" />
        <link rel="stylesheet" href="http://codemirror.net/doc/docs.css" type="text/css" media="screen" />
        <script src="http://code.guru99.com/php/lib/codemirror.js"></script>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">></script>
        <link rel="stylesheet" href="http://code.guru99.com/Sanjay/lib/codemirror.css" type="text/css" media="screen" />
        <script type="text/javascript" src="http://code.guru99.com/perl/perl.js"></script>
        <script type="text/javascript" src="cperl.js"></script>
        <script src="code.js"></script>
</head>
<body>
<style type="text/css">
.CodeMirror {
    border: 1px solid #eee;
    height: auto;
    width : 630px;
}
.CodeMirror-scroll {
    height: auto;
    overflow-y: hidden;
    overflow-x: auto;
}
</style>
<p>Integer : whole numbers e.g. -3, 0, 69. The maximum value of an integer is platform-dependent. On a 32 bit machine, its usually around 2 billion. 64 bit machines usually have larger values. The constant PHP_INT_MAX is used to determine the maximum value.</p>
<pre class="codeguru">say 'hi';</pre>
Let us now look at how PHP determines the data type depending on the attributes of the supplied data.
<pre class="codeguru">say 'hello';</pre>
Floating point numbers
<pre class="codeguru">say 'you r amazing';</pre>
Character strings
<pre class="codeNrun">say 'i am fine';</pre>
<form class="hidden code-box" method="GET" name="sample">
    <div dir="ltr">
        <textarea class="php" name="codeguru"></textarea>
    </div>
    <input type="button" value="Run" />
    <br/>
    <br/>Output:
    <br/>
    <br/>
    <textarea id="print-result4" disabled="true" cols="77"></textarea>
    <br/>
</form>
<form class="hidden code-box" method="GET" name="Nosample">
    <div dir="ltr">
        <textarea class="php" name="codeNrun"></textarea>
    </div>
</form>
</body>
</html>

code.js文件:

$(document).ready(function () {
    var idIndex = 0;
    $('pre.codeguru').each(function () {
        var pre = this;
        var form = $('form[name=sample]').clone();
        $(form).removeAttr('name');
        $(form).removeClass('hidden');
        $($(form).find('textarea')[0]).val($(pre).text());
        var id = $(pre).attr('id');
        $(form).find('div textarea[name=code]').first().attr('id', id);
        $(form).find('#print-result4').first().attr('id', 'print-result' + idIndex++);
        $(pre).replaceWith(form);
    });
    var n = 0;
    $('input[type=button]').each(function () {
        $(this).click(function (x) {
            return function () {
                execute(x);
            };
        }(n++))
    });
    window.editors = [];
    $('textarea[name=codeguru]').each(function () {
        window.editor = CodeMirror.fromTextArea(this, {
            lineNumbers: true,
            matchBrackets: true,
            mode: "perl",
            tabMode: "shift"
        });
        editors.push(editor);
    });
     $('pre.codeNrun').each(function () {
        var pre = this;
        var form = $('form[name=Nosample]').clone();
        $(form).removeAttr('name');
        $(form).removeClass('hidden');
        $($(form).find('textarea')[0]).val($(pre).text());
        var id = $(pre).attr('id');
        $(form).find('div textarea[name=codeNrun]').first().attr('id', id);
        $(pre).replaceWith(form);
    });
    window.editors = [];
    $('textarea[name=codeNrun]').each(function () {
            window.editor = CodeMirror.fromTextArea(this, {
            lineNumbers: true,
            matchBrackets: true,
            mode: "perl",
            tabMode: "shift"
        });
        editors.push(editor);
    });
});
function execute(idx) {
    p5pkg.CORE.print = function (List__) {
        var i;
        for (i = 0; i < List__.length; i++) {
            document.getElementById('print-result' + idx).value += p5str(List__[i])
        }
        return true;
    };
    p5pkg["main"]["v_^O"] = "browser";
    p5pkg["main"]["Hash_INC"]["Perlito5/strict.pm"] = "Perlito5/strict.pm";
    p5pkg["main"]["Hash_INC"]["Perlito5/warnings.pm"] = "Perlito5/warnings.pm";
    var source = editors[idx].getValue();
    var pos = 0;
    var ast;
    var match;
    document.getElementById('print-result' + idx).value = "";
    try {
        var start = new Date().getTime();
        var js_source = p5pkg["Perlito5"].compile_p5_to_js([source]);
        var end = new Date().getTime();
        var time = end - start;
        // run
        start = new Date().getTime();
        eval(js_source);
        end = new Date().getTime();
        time = end - start;
    } catch (err) {
        //document.getElementById('log-result').value += "Error:'n";
    }
}

所以现在你可以看到有两个表单名称sample和Nosample,就像这个

<form class="hidden code-box" method="GET" name="sample">

<form class="hidden code-box" method="GET" name="Nosample">

像这种有两种类型的预标签codeguru和codeNrun

<pre class="codeguru">say 'you r amazing';</pre>

<pre class="codeNrun">say 'i am fine';</pre>

所以,当我用表单替换这个预代码时,当我试图运行时,输出被打印在另一个文本区域中,我不明白为什么会发生这种情况。

您的代码中有一个额外的window.editors = [];

首先创建一个editors数组,然后将其推送一些CodeMirror对象,然后再次将editors重新定义为空数组。。。只需移除第二条window.editors = [];线。

第二:这里id变量的实际值是多少?

$(form).find('div textarea[name=code]').first().attr('id', id);

您正在读取textareaid,但看起来像是undefined,因为HTML中没有id

这一行中有一个类似的错误:

$(form).find('div textarea[name=codeNrun]').first().attr('id', id);

我想这里的id应该是'print-result3',但现在是undefined

请注意,id正文的附加数字是按照$('input[type=button]').each()提供的顺序添加的。这应该是文档中元素的顺序。请注意,原来的button是您在屏幕上看到的最后一个。


我认为我的回答中缺乏解释,如何将按钮指向编辑器和相应的print-resultX。我试着在这里解释一下:

在原始<input id="print-resultX" ... />中,X应该是print-result输入总数,即CodeMirror对象的数量,因为当您克隆form时,所有克隆都会放在此原始形式之前。在克隆过程中,相应的X会添加到每个id值的主体中。

X值作为参数idx传递给execute(idx)函数,即单击的按钮"知道"应在execute()中处理哪个编辑器,因为X也表示editors数组中的索引。

idx添加到id的主体(='print-result')时,可以将操作定向到特定的打印结果字段。