这是一个由两部分组成的问题。前几天有人回答了一个类似的问题(它也包含了PHP中这种类型的数组的信息),但我找不到它。
1.)首先,对于在表单中输入标记的name元素末尾创建的数组,正确的术语是什么?
<form>
<input name="p_id[]" value="0"/>
<input name="p_id[]" value="1"/>
<input name="p_id[]" value="2"/>
</form>
2.)如何使用JavaScript从该数组中获取信息?具体地说,我现在只想计算数组的元素。下面是我所做的,但它不起作用。
function form_check(){
for(var i = 0; i < count(document.form.p_id[]); i++){ //Error on this line
if (document.form.p_name[i].value == ''){
window.alert('Name Message');
document.form.p_name[i].focus();
break;
}
else{
if (document.form.p_price[i].value == ''){
window.alert('Price Message');
document.form.p_price[i].focus();
break;
}
else{
update_confirmation();
}
}
}
}
发布于 2010-07-13 13:32:40
试着这样做:
var p_ids = document.forms[0].elements["p_id[]"];
alert(p_ids.length);
for (var i = 0, len = p_ids.length; i < len; i++) {
alert(p_ids[i].value);
}
发布于 2010-07-13 12:35:00
document.form.p_id.length
...不是count()。
你真的应该给你的表单一个id
<form id="myform">
然后使用以下命令引用它:
var theForm = document.getElementById("myform");
然后引用这些元素,如:
for(var i = 0; i < theForm.p_id.length; i++){
发布于 2017-08-06 16:30:00
下面是一些PHP和JavaScript演示代码,展示了一种在表单上创建索引字段(同名字段),然后在JavaScript和PHP中处理它们的简单方法。字段必须同时具有"ID“名称和"NAME”名称。Javascript使用ID,PHP使用名称。
<?php
// How to use same field name multiple times on form
// Process these fields in Javascript and PHP
// Must use "ID" in Javascript and "NAME" in PHP
echo "<HTML>";
echo "<HEAD>";
?>
<script type="text/javascript">
function TestForm(form) {
// Loop through the HTML form field (TheId) that is returned as an array.
// The form field has multiple (n) occurrences on the form, each which has the same name.
// This results in the return of an array of elements indexed from 0 to n-1.
// Use ID in Javascript
var i = 0;
document.write("<P>Javascript responding to your button click:</P>");
for (i=0; i < form.TheId.length; i++) {
document.write(form.TheId[i].value);
document.write("<br>");
}
}
</script>
<?php
echo "</HEAD>";
echo "<BODY>";
$DQ = '"'; # Constant for building string with double quotes in it.
if (isset($_POST["MyButton"])) {
$TheNameArray = $_POST["TheName"]; # Use NAME in PHP
echo "<P>Here are the names you submitted to server:</P>";
for ($i = 0; $i <3; $i++) {
echo $TheNameArray[$i] . "<BR>";
}
}
echo "<P>Enter names and submit to server or Javascript</P>";
echo "<FORM NAME=TstForm METHOD=POST ACTION=" ;
echo $DQ . "TestArrayFormToJavascript2.php" . $DQ . "OnReset=" . $DQ . "return allowreset(this)" . $DQ . ">";
echo "<FORM>";
echo "<INPUT ID = TheId NAME=" . $DQ . "TheName[]" . $DQ . " VALUE=" . $DQ . "" . $DQ . ">";
echo "<INPUT ID = TheId NAME=" . $DQ . "TheName[]" . $DQ . " VALUE=" . $DQ . "" . $DQ . ">";
echo "<INPUT ID = TheId NAME=" . $DQ . "TheName[]" . $DQ . " VALUE=" . $DQ . "" . $DQ . ">";
echo "<P><INPUT TYPE=submit NAME=MyButton VALUE=" . $DQ . "Submit to server" . $DQ . "></P>";
echo "<P><BUTTON onclick=" . $DQ . "TestForm(this.form)" . $DQ . ">Submit to Javascript</BUTTON></P>";
echo "</FORM>";
echo "</BODY>";
echo "</HTML>";
https://stackoverflow.com/questions/3234205
复制相似问题