drj14664 2014-02-12 17:33
浏览 124
已采纳

Javascript数组如何为数组中的每一行执行一个函数

okay so here's my current script

<html>
<head>


    <script type="text/javascript">
        function textareaToArray(t){
        return t.value.split(/[
]+/);
        }
        function showArray(msg){
        document.getElementById("message").innerHTML = msg.join("&#013;");
        }
    </script>


    </head>
        <title>
        Welcome To ....
        </title>
<body>
        <center>
        <h1> WELCOME TO .... </h1></center>

    </br>
    </br>
    </br>

<center>

        <form>
        <textarea rows="10" cols="60" name="alpha"></textarea>
        <br>
        <input type="button"
        value="show array"
        onclick="showArray(textareaToArray(this.form.alpha ))">
        </form>
</center>
    </br>
<center>
        <textarea id="message" rows="6" cols="60" name="message"></textarea>
</center>
</body>
</html>

as you can see the javascript function basically just outputs the array into the second text area.

how can I invoke a function for each line in the array

e.g.

a function to use line in array post to php script as post data then echo the result back after this it does the same for the next line can any one help me on how I would post each line to a php script and echo the response back into the text area

  • 写回答

2条回答 默认 最新

  • doumor942473 2014-02-12 18:07
    关注

    I redid your entire example, also fixed some other small errors in your html.

    <html><head><title>Welcome To ....</title>
        <script type="text/javascript">
            function textareaToArray(t){
                return t.value.split(/[
    ]+/);
            }
            function showArray(msg){
                for(i = 0; i < msg.length; i++) {
                    // something per item
                    alert(msg[i]);
                }
                // the old code
                document.getElementById("message").innerHTML = msg.join("&#013;");
            }
        </script>
    </head>
    <body>
        <h1> WELCOME TO .... </h1>
        <form>
        <textarea rows="10" cols="60" name="alpha"></textarea>
        <br>
        <input type="button" value="show array" onclick="showArray(textareaToArray(this.form.alpha ))">
        </form>
        <br>
        <textarea id="message" rows="6" cols="60" name="message"></textarea>
    </body></html>
    

    Where the "alert" happens is where you could add code for each item in the array. Now for the warning, there is no input checking.. this code is brutally insecure and if you are allowing to pass things from a textarea, this is definitely a vector for XSS attacks.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容