简体   繁体   English

Javascript数组如何为数组中的每一行做一个功能

[英]Javascript array how do i do a function for each line in array

okay so here's my current script 好的,这是我当前的脚本

<html>
<head>


    <script type="text/javascript">
        function textareaToArray(t){
        return t.value.split(/[\n\r]+/);
        }
        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. 如您所见,javascript函数基本上只是将数组输出到第二个文本区域。

how can I invoke a function for each line in the array 我如何为数组中的每一行调用一个函数

eg 例如

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 一种使用数组中的行作为php数据作为发布数据的函数,然后回显结果,此操作对下一行相同。任何人都可以帮助我如何将每一行发布到php脚本并回显响应进入文本区域

I redid your entire example, also fixed some other small errors in your html. 我重做了整个示例,还修复了html中的其他一些小错误。

<html><head><title>Welcome To ....</title>
    <script type="text/javascript">
        function textareaToArray(t){
            return t.value.split(/[\n\r]+/);
        }
        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. 现在为警告起见,没有输入检查。.此代码是不安全的,如果允许从textarea传递内容,则绝对是XSS攻击的载体。

正确答案是: for (var i = 0; i < array.length; i++) alert(array[i])

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

相关问题 如何使用 JSON.stringify 输出一个 javascript 数组,其中每个数组项都是一个新行 - How do I output a javascript array where each array item is a new line using JSON.stringify 如何使用 .txt 文件并将每一行作为数组读取? - How do I use a .txt file and read each line as an array? 如何将函数返回的数组复制到 JavaScript 中的另一个数组中? - How do I copy an array returned by a function into another array in JavaScript? 如何使用JavaScript延迟对数组中每个元素执行的函数? - How do I delay a function executing for each element in an array using JavaScript? 如何为数组/ JavaScript中的每个项目创建不同的按钮 - How do I create different button for each item in an array / JavaScript 如何将数组的每个成员乘以javascript中的变量标量? - How do I multiply each member of an array by a variable scalar in javascript? 如何向 Javascript 数组中的每个文本值添加文本? - How do I add text to each text value in a Javascript Array? 如何将数组的每个成员乘以javascript中的标量? - How do I multiply each member of an array by a scalar in javascript? 如何将Ruby数组传递给Javascript以生成折线图 - How do I pass a Ruby array to Javascript to make a line graph 如何使数组中的每个元素成为另一个数组? - How do I make each each element in array another array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM