简体   繁体   English

如何获取textarea的当前值? 可以使用哪种方法代替$(“ form”)。submit(function(){?

[英]How to I retrieve a current value of textarea ? which method instead of $(“form”).submit(function(){ can be used?

This is my code: 这是我的代码:

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

problem : so I have alert the value of text area by: 问题:所以我通过以下方式提醒了文本区域的值:

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

but the value of text area is alert is load at the time of page load.and I want to alert current value of textarea. 但是文本区域的值是alert是在页面加载时加载的。我想警告textarea的当前值。

I have also tried this: 我也尝试过这个:

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

but that also not useful. 但这也没有用。

How can I do this? 我怎样才能做到这一点?

您可以使用

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

Try to use 尝试使用

$("#source").val() to get value of textarea $("#source").val()获得textarea的值

Thanks 谢谢

I suppose your problem might be occuring because of the Editor you are using. 我想您正在使用的编辑器可能会引起您的问题。

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

You should be able to retrieve the value with: 您应该可以使用以下方法检索值:

editor.getValue()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何检索textarea的当前值? - how can i retrieve a current value of textarea? 如何提交表单? 用于代替 = 在 GET 方法上 - How to submit form in which ? is used instead of = on GET method 如何在nodejs中提交表单获取textarea的值 - How to get value of textarea on submit of form in nodejs 如何使用span而不是textarea来使函数工作? - How can I make function work with span instead of textarea? ReactJS:如何将检索到的 post json 响应显示到我用来提交值的同一个表单组件? - ReactJS: How do i display the retrieved post json response to the same form component which i used to submit the value? 如何在gRaphael线图hoverColumn函数中检索当前的X值? - How can I retrieve current X-value in gRaphael linechart hoverColumn function? 如何使用 get 方法提交未在 url 中添加值的表单 - How can I submit a form with get method where value not added in url 您如何确定使用jQuery提交表单的提交? - How do you determine which submit was used to submit a form with jQuery? 当表单提交被禁用时,如何在文本区域上按Enter下行 - How can I press enter go down line on textarea when form submit is disable keypress enter 当我按下提交按钮时,如何将值存储在HTML中 <textarea> 在JavaScript变量中? - When I press submit button, how can I store the value in a html <textarea> in a JavaScript variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM