我正在构建一个表单,其中JavaScript允许字段重复与输入到第一个表单输入字段中的数字相等的次数,如第一名乘客名、第一名乘客姓、第二名乘客姓名等。一旦按下提交按钮,表单就会发送到下一页。表单包含从上一页张贴的隐藏字段。这些隐藏字段被张贴在正确的值上,这使我相信表单生成代码中有一个逻辑错误。在表单发布到的页面上,这些值与开发人员工具中阅读value=的“nopass字段的”中的HTML代码是空白的。请注意,代码不会产生语法错误,除了不投递之外,还会按照预期的方式进行操作,在更新影响到它的字段时,会出现正确的字段数量,并正确更新文本。
代码:
HTML表单
<form name="bookpassdet" id="bookpassdet" method="post" action="bookconf.php">
<fieldset>
<legend>Booking Reference no. <?php echo $bookref; ?></legend>
<label class="amtofpass" for="amountofpassengers">Amount of Passengers</label>
<input type="hidden" name="bookref" id="bookref" value="<?php echo $bookref?>">
<input type="hidden" name="fromob" id="fromob" value="<?php echo $from?>">
<input type="hidden" name="goingob" id="goingob" value="<?php echo $goingto?>">
<input type="hidden" name="monthob" id="monthob" value="<?php echo $month?>">
<input type="hidden" name="dayob" id="dayob" value="<?php echo $day?>">
<input type="text" name ="amountofpassengers" id="nopass">
<input type="checkbox" name="wheelchair" value="yes"> Wheelchair Required<br>
<div id="container"/>
</fieldset>
</form>JavaScript生成输入字段
function addPassengers(e){
var pasobj=document.getElementById('nopass').value;
var container = document.getElementById("container");
while (container.hasChildNodes()){
container.removeChild(container.lastChild);
}
for(var i =0;i<pasobj;i++){
container.appendChild(document.createTextNode("Passenger " + (i+1)+" Forename"));
var input = document.createElement("input");
input.type = "text";
input.id = ("pasforname"+i);
container.appendChild(input);
container.appendChild(document.createElement("br"));
container.appendChild(document.createTextNode("Passenger " + (i+1)+" Surname"));
var input = document.createElement("input");
input.type = "text";
input.id = ("passurname"+i);
container.appendChild(input);
container.appendChild(document.createElement("br"));
container.appendChild(document.createTextNode("Passenger " + (i+1)+" Age"));
var agearr = ["<2","3-10","11-16",">16"];
var selectlist = document.createElement("select");
selectlist.id=("myselect"+i);
selectlist.name="myselect";
container.appendChild(selectlist);
container.appendChild(document.createElement("br"));
for(var k =0; k<agearr.length; k++){
var option = document.createElement("option");
option.value = agearr[k];
option.text = agearr[k];
selectlist.appendChild(option);
}
var single_button = makeRadioButton(("journey"+i),"single","Single Journey",("single"+i));
var return_button = makeRadioButton(("journey"+i),"return","Return Journey",("return"+i));
container.appendChild(single_button);
container.appendChild(return_button);
container.appendChild(document.createElement("br"));
container.appendChild(document.createTextNode("Cost: "));
var text = document.createElement("p");
text.id = ("costp"+i);
container.appendChild(text);
container.appendChild(document.createElement("br"));
}
var submit =document.createElement("input");
submit.type = "submit";
submit.id="subbutton";
submit.value="test";
container.appendChild(submit);
//add event listener to each created element in a loop where
if(window.addEventListener) {
for(var m=0;m<pasobj;m++){
document.getElementById("myselect"+m).addEventListener("change",calculateCost,false);
document.getElementById("single"+m).addEventListener("change",calculateCost,false);
document.getElementById("return"+m).addEventListener("change",calculateCost,false);
}
document.getElementById("subbutton").addEventListener("click",submitForm,false);
} else if(window.attachEvent) {
for(var m=0; m<pasobj;m++){
document.getElementById("myselect"+m).addEventListener("change",calculateCost);
document.getElementById("single"+m).addEventListener("change",calculateCost);
document.getElementById("return"+m).addEventListener("change",calculateCost);
}
document.getElementById("subbutton").addEventListener("click",submitForm);
}
}
there are 3 other functions called makeRadioButton(name,value,text, id);
calculateCost(e);
prepareBookPassDet();prepareBookPassDet只是在keyup事件调用addPassengers函数时将事件侦听器附加到nopass窗体字段
我尝试过的:
我试图通过使用请求而不是post来处理php方面的问题,但没有结果。
我已经能够将一个事件侦听器附加到submit按钮,该按钮创建一个弹出显示输入到nopass中的值,并显示正确的值。
这一点,再加上被发布的隐藏字段,我相信输入到字段中的值没有被提交,但是表单是。
谢谢你抽出时间来读这个。我对web特别是javascript缺乏经验,所以我希望任何解决方案都能用简单的术语来解释,并且不包括框架,坚持使用基本的php、html和JavaScript。再一次谢谢。
发布于 2019-02-24 18:07:36
要与表单数据一起发送,输入元素需要有一个name属性。id在这个地方不生效。这是因为可以有多个相同的名称,例如单选按钮,但是在HTML中只有唯一的in是有效的。
在您的代码中:
var input = document.createElement("input");
input.type = "text";
input.id = 'pasforname'+i;
input.setAttrubute('name', 'pasforname'+i); https://stackoverflow.com/questions/54854784
复制相似问题