如何检索textarea的当前值?哪个方法代替$(" form")。submit(function(){可以用吗?

时间:2013-07-08 10:20:21

标签: javascript jquery ajax textarea

这是我的代码:

<html>
    <head>
    <title>Perl WEB</title>
    <script type="text/javascript" src="/Perl1/codemirror.js"></script>
    <link rel="stylesheet" href="/Perl1/codemirror.css"/>
    <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
    <script type="text/javascript" src="http://code.guru99.com/perl/perl.js"></script>
    <style>
    .CodeMirror {
    border: 1px solid #eee;
    }   
    .CodeMirror-scroll {
    height: auto;
    overflow-y: hidden;
    overflow-x: auto;
    }
</style>
<script>
$(document).ready(function(){
  $("form").submit(function(){
    alert("Submitted");
  });
});
</script>
<script type="text/javascript">

    function execute() {
            p5pkg.CORE.print = function(List__) {
                var i;
                for (i = 0; i < List__.length; i++) {
                  document.getElementById('print-result').value += p5str(List__[i])
                }
                return true;
            };
            p5pkg.CORE.warn = function(List__) {
                var i;
                List__.push("\n");
                for (i = 0; i < List__.length; i++) {
                    document.getElementById('log-result').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 = document.getElementById('source').value;
            alert(source);
            var pos = 0;
            var ast;
            var match;
            document.getElementById('log-result').value   = "";
        //  document.getElementById('js-result').value    = "";
            document.getElementById('print-result').value = "";
            try {
                // compile
                document.getElementById('log-result').value += "Compiling.\n";
                var start = new Date().getTime();
                var js_source = p5pkg["Perlito5"].compile_p5_to_js([source]);
                var end = new Date().getTime();
                var time = end - start;
                document.getElementById('log-result').value +=  "Compilation time: " + time + "ms\n";
        //      document.getElementById('js-result').value  += js_source + ";\n";

                // run
                start = new Date().getTime();
                eval(js_source);
                end = new Date().getTime();
                time = end - start;
                document.getElementById('log-result').value += "Running time: " + time + "ms\n";

                p5pkg.CORE.print(["\nDone.\n"]);
            }
            catch(err) {
                document.getElementById('log-result').value += "Error:\n";
                document.getElementById('log-result').value += err + "\n";
                document.getElementById('log-result').value += "Compilation aborted.\n";
                  }
        }
    </script>
    </head>
    <body>
    <form>
<textarea id="source" cols="70" rows="10">
say 'h';
</textarea>
    <div class="hint">This code is editable. Click Run to execute.</div>
    <input type="button" value="Run" onclick="execute()"/></br>
Output:</br>
    <textarea id="print-result" disabled="true" rows="10" cols="70"></textarea></br>
Log:</br>
    <textarea  id="log-result" disabled="true" cols="70"></textarea>
    <script>
      var editor = CodeMirror.fromTextArea(document.getElementById("source"), {
        lineNumbers: true,
        indentUnit: 4,
        indentWithTabs: true,
        enterMode: "keep",
        tabMode: "shift"
      });
    </script>
</form>
    </body>
</html>

问题:所以我通过以下方式提醒文本区域的值:

var source = document.getElementById('source').value;
            alert(source);

但文本区域的值是警告是在页面加载时加载。我想提醒textarea的当前值。

我也试过这个:

$("form").submit(function())

但这也没用。

我该怎么做?

3 个答案:

答案 0 :(得分:0)

你可以使用

$("#source").val();

答案 1 :(得分:0)

尝试使用

$("#source").val()获取textarea的值

由于

答案 2 :(得分:0)

我认为您的问题可能因为您正在使用的编辑而发生。

http://codemirror.net/doc/manual.html

您应该能够使用以下方法检索值:

editor.getValue()