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

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站