如何在javascript数组中输入HTML输入?
我正在用html创建一个表单,我想在javascript数组中添加表单值。我怎么能做到如何在javascript数组中输入HTML输入?,javascript,jquery,html,arrays,Javascript,Jquery,Html,Arrays,我正在用html创建一个表单,我想在javascript数组中添加表单值。我怎么能做到 <!DOCTYPE html> <html> <head> <script src="array.js" type="text/JavaScript"></script> </head> <body> <h2>Input Array</h2>
<!DOCTYPE html>
<html>
<head>
<script src="array.js" type="text/JavaScript"></script>
</head>
<body>
<h2>Input Array</h2>
<form method="post">
<input type="text" id="first"/>
<input type="text" id="second"/>
<input type="text" id="third"/>
<input type="text" id="fourth" />
<input type="button" value="Submit" OnClick="array();"/>
</form>
</body>
</html>
<script type="text/javascript">
function abc()
{
formdata= $("#form").serialize();
alert(formdata);
}
</script>
</head>
<body>
<h2>Input Array</h2>
<form method="post" id="form">
<input type="text" id="first" name="a" />
<input type="text" id="second" name="b"/>
<input type="text" id="third" name="c"/>
<input type="text" id="fourth" name="d" />
<input type="button" value="Submit" OnClick="return abc();"/>
</form>
</body> </html>
输入阵列
javascript数组中的这四个值并将它们打印在同一页上。您可以使用:
<script type="text/javascript">
function abc()
{
formdata= $("#form").serialize();
alert(formdata);
}
</script>
</head>
<body>
<h2>Input Array</h2>
<form method="post" id="form">
<input type="text" id="first" name="a" />
<input type="text" id="second" name="b"/>
<input type="text" id="third" name="c"/>
<input type="text" id="fourth" name="d" />
<input type="button" value="Submit" OnClick="return abc();"/>
</form>
</body> </html>
例如:
$( "form" ).submit(function( event ) {
console.log( $( this ).serializeArray() );
event.preventDefault();
});
<script type="text/javascript">
function abc()
{
formdata= $("#form").serialize();
alert(formdata);
}
</script>
</head>
<body>
<h2>Input Array</h2>
<form method="post" id="form">
<input type="text" id="first" name="a" />
<input type="text" id="second" name="b"/>
<input type="text" id="third" name="c"/>
<input type="text" id="fourth" name="d" />
<input type="button" value="Submit" OnClick="return abc();"/>
</form>
</body> </html>
这将产生如下结果:
[
{
name: "a",
value: "1"
},
{
name: "b",
value: "2"
},
{
name: "c",
value: "3"
},
{
name: "d",
value: "4"
},
{
name: "e",
value: "5"
}
]
<script type="text/javascript">
function abc()
{
formdata= $("#form").serialize();
alert(formdata);
}
</script>
</head>
<body>
<h2>Input Array</h2>
<form method="post" id="form">
<input type="text" id="first" name="a" />
<input type="text" id="second" name="b"/>
<input type="text" id="third" name="c"/>
<input type="text" id="fourth" name="d" />
<input type="button" value="Submit" OnClick="return abc();"/>
</form>
</body> </html>
注:
<script type="text/javascript">
function abc()
{
formdata= $("#form").serialize();
alert(formdata);
}
</script>
</head>
<body>
<h2>Input Array</h2>
<form method="post" id="form">
<input type="text" id="first" name="a" />
<input type="text" id="second" name="b"/>
<input type="text" id="third" name="c"/>
<input type="text" id="fourth" name="d" />
<input type="button" value="Submit" OnClick="return abc();"/>
</form>
</body> </html>
不要忘记在输入元素中传递名称。例如:
<script type="text/javascript">
function abc()
{
formdata= $("#form").serialize();
alert(formdata);
}
</script>
</head>
<body>
<h2>Input Array</h2>
<form method="post" id="form">
<input type="text" id="first" name="a" />
<input type="text" id="second" name="b"/>
<input type="text" id="third" name="c"/>
<input type="text" id="fourth" name="d" />
<input type="button" value="Submit" OnClick="return abc();"/>
</form>
</body> </html>
<script type="text/javascript">
function abc()
{
formdata= $("#form").serialize();
alert(formdata);
}
</script>
</head>
<body>
<h2>Input Array</h2>
<form method="post" id="form">
<input type="text" id="first" name="a" />
<input type="text" id="second" name="b"/>
<input type="text" id="third" name="c"/>
<input type="text" id="fourth" name="d" />
<input type="button" value="Submit" OnClick="return abc();"/>
</form>
</body> </html>
函数abc()
{
formdata=$(“#form”).serialize();
警报(formdata);
}
输入阵列
这是对html代码的一些修改
<script type="text/javascript">
function abc()
{
formdata= $("#form").serialize();
alert(formdata);
}
</script>
</head>
<body>
<h2>Input Array</h2>
<form method="post" id="form">
<input type="text" id="first" name="a" />
<input type="text" id="second" name="b"/>
<input type="text" id="third" name="c"/>
<input type="text" id="fourth" name="d" />
<input type="button" value="Submit" OnClick="return abc();"/>
</form>
</body> </html>
<!DOCTYPE html>
<html>
<head>
<script src="array.js" type="text/JavaScript"></script>
</head>
<body>
<h2>Input Array</h2>
<form method="post">
<input type="text" id="first" class="vals"/>
<input type="text" id="second" class="vals"/>
<input type="text" id="third" class="vals"/>
<input type="text" id="fourth" class="vals"/>
<input type="text" id="fifth" class="vals"/>
<input type="button" value="Submit" OnClick="array();"/>
</form>
<p id="temp"></p>
</body>
</html>
输入阵列
<script type="text/javascript">
function abc()
{
formdata= $("#form").serialize();
alert(formdata);
}
</script>
</head>
<body>
<h2>Input Array</h2>
<form method="post" id="form">
<input type="text" id="first" name="a" />
<input type="text" id="second" name="b"/>
<input type="text" id="third" name="c"/>
<input type="text" id="fourth" name="d" />
<input type="button" value="Submit" OnClick="return abc();"/>
</form>
</body> </html>
您的javascript函数如下所示
<script type="text/javascript">
function abc()
{
formdata= $("#form").serialize();
alert(formdata);
}
</script>
</head>
<body>
<h2>Input Array</h2>
<form method="post" id="form">
<input type="text" id="first" name="a" />
<input type="text" id="second" name="b"/>
<input type="text" id="third" name="c"/>
<input type="text" id="fourth" name="d" />
<input type="button" value="Submit" OnClick="return abc();"/>
</form>
</body> </html>
function array(){
var arr = [];
var valElements = document.getElementsByClassName("vals");
var tempstr = "";
/* retrieve the input from textbox
and store in an array
*/
for(i=0; i<valElements.length; i++){
arr[i] = valElements[i].value;
tempstr = tempstr+arr[i];
}
/* use the input on your page */
var a = document.getElementById("temp");
a.innerHTML = tempstr;
}
函数数组(){
var-arr=[];
var valElements=document.getElementsByClassName(“VAL”);
var tempstr=“”;
/*从文本框中检索输入
并存储在一个数组中
*/
对于(i=0;i目的是什么?为什么不直接使用serialize()?我是javascript新手。请定义。为什么要数组?我只想在javascript数组中获取所有内容,然后显示在html页面上。之后,我将在我的google map api中使用这些数组来绘制标记。感谢buddy提供了此方法。但是,你能告诉我可以添加吗“萨博”、“沃尔沃”值通过HTML输入var cars=[“萨博”、“沃尔沃”、“宝马”];很抱歉,无法获取您。但当我不知道值的数量时?为什么必须为文本框指定id?相反,您可以将它们分类,然后循环。