[英]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("
");
}
</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("
");
}
</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.