我在javascript中创建了一个附加一个包含5个元素的表的对象。但是,当我提交表单时,添加的字段不会显示。
var whenCellOne = firstRow.insertCell(1);
this.often = document.createElement("select");
this.often.name = "['"+me.id+"']['often']";
this.often.appendChild(this.createOption("Vecka", "0"));
this.often.appendChild(this.createOption("Månad", "1"));
this.often.onchange = function(){ me.update(); };
whenCellOne.appendChild(this.often);
这就是我创建其中一个元素的方法,我希望它会以$_POST['1a']['often'] = "0"
的形式出现,但var_dump看起来像array(1) { ["submit"]=> string(2) "ok" }
这有什么不对?以及如何修复它?
以下是完整的javascript代码http://pastebin.com/Jai3gChv,这是测试页http://pastebin.com/F3rgBumh
答案 0 :(得分:1)
PHP不会使用名称格式为$_POST
的字段填充[something]
。您需要更改名称。
如果您希望$_POST['1a']['often']
拥有值,则需要name="1a[often]"
。
假设me.id
的值为1a
,您目前将其设为name="['1a']['often']"
this.often.name = "['"+me.id+"']['often']";
应该是
this.often.name = me.id + "[often]";
答案 1 :(得分:1)
您的脚本中还有一些错误,您还必须实现Quentin提到的错误修复:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="./system/advancedDate.js"></script>
</head>
<body>
<form action="#" method="POST">
<div id='1a'></div>
<input type="submit" name="submit" value="ok">
</form>
</body>
<footer>
<script type="text/javascript">
window.onload = function(){
var ad = new advancedDate("1a");
ad.appendInElement();
}
</script>
</footer>
</html>
function advancedDate(id){
this.id = id;
this.createDOMElements();
}
advancedDate.prototype.createDOMElements = function(){
var me = this;
this.table = document.createElement("table");
this.table.style.border = "Solid black 3px";
//Create row 1
var firstRow = this.table.insertRow(0);
var secondRow = this.table.insertRow(1);
var thirdRow = this.table.insertRow(2);
var fourthRow = this.table.insertRow(3);
var whenCellOne = firstRow.insertCell(0);
this.repeat = document.createElement("select");
this.repeat.name = "['"+me.id+"']['repeat']";
this.repeat.appendChild(this.createOption("Varje", "0"));
this.repeat.appendChild(this.createOption("Kommande", "1"));
whenCellOne.appendChild(this.repeat);
whenCellOne = firstRow.insertCell(1);
this.often = document.createElement("select");
this.often.name = "["+me.id+"][often]";
this.often.appendChild(this.createOption("Vecka", "0"));
this.often.appendChild(this.createOption("Månad", "1"));
this.often.onchange = function(){ me.update(); };
whenCellOne.appendChild(this.often);
secondRow.insertCell(0);
var dateCell = secondRow.insertCell(1);
this.when = document.createElement("select");
this.when.name = "['"+me.id+"']['when']";
this.when.setAttribute("multiple","true");
this.when.style.width = "300px";
dateCell.appendChild(this.when);
var dayCell = thirdRow.insertCell(0);
dayCell.innerHTML = "Day:";
var dayInputCell = thirdRow.insertCell(1);
this.dayInput = document.createElement("input");
this.dayInput.name = "['"+me.id+"']['when']";
this.dayInput.setAttribute("type", "text");
this.dayInput.onkeyup = function(){ me.update(); };
this.dayInput.onblur = function(){ me.update(); };
dayInputCell.appendChild(this.dayInput);
var workCell = fourthRow.insertCell(0);
workCell.innerHTML = "Arbetstid:";
var workTimeInputCell = fourthRow.insertCell(1);
this.workTimeInput = document.createElement("input");
this.workTimeInput.name = "['"+me.id+"']['workTime']";
this.workTimeInput.setAttribute("type", "text");
this.workTimeInput.onkeyup = function(){ me.calculateTime(this); };
this.workTimeInput.onblur = function(){ me.calculateTime(this); };
workTimeInputCell.appendChild(this.workTimeInput);
this.update();
};
advancedDate.prototype.createOption = function(text, value){
var e = document.createElement("option");
e.setAttribute("value",value);
e.innerHTML = text;
return e;
};
advancedDate.prototype.calculateTime = function(element){
var pattern = /([0-9]{2}):([0-9]{2})([^0-9]+)([0-9]{2}):([0-9]{2})/g;
if(element.value.match(pattern)){
var result = pattern.exec(element.value);
var time1 = (parseInt(result[1]) * 3600) + (parseInt(result[2]) * 60);
var time2 = (parseInt(result[4]) * 3600) + (parseInt(result[5]) * 60);
var max = Math.max(time1, time2);
var min = Math.min(time1, time2);
var time = max-min;
element.value = Number((time/3600).toFixed(2));
}
};
advancedDate.prototype.update = function(){
var currentDay;
try{
currentDay = parseInt(this.dayInput.value);
}catch(err){
currentDay = -1;
}
var d = new Date();
var month = d.getMonth();
var year = parseInt(d.getFullYear())+1;
var day = d.getDate();
var months = [
["01, Januari", "January", 0],
["02, Februari", "February", 1],
["03, Mars", "March", 2],
["04, April", "April", 3],
["05, Maj", "May", 4],
["06, Juni", "June", 5],
["07, Juli", "July", 6],
["08, Augusti", "August", 7],
["09, September", "September", 8],
["10, Oktober", "October", 9],
["11, November", "November", 10],
["12, December", "December", 11]];
var weeks = [
["Måndag", "Monday" ],
["Tisdag", "Tuesday" ],
["Onsdag", "Wednesday" ],
["Torsdag", "Thursday" ],
["Fredag", "Friday" ],
["Lördag", "Saturday" ],
["Söndag", "Sunday" ]];
//Save the selected values
var selected = [];
try{
for(var i=0; i<this.when.options.length; i++){
if(this.when.options[i].selected)
selected.push(this.when.options[i].value);
}
}catch(err){
}
if(this.often.value === "0"){
this.when.options.length = 0;
for(var x=0; x<weeks.length; x++)
this.when.appendChild(this.createOption(weeks[x][0], weeks[x][1]));
//Make extra invisable
this.table.rows[2].style.display = 'none';
this.dayInput.value = "";
}
if(this.often.value === "1"){
this.when.options.length = 0;
for(var y=0; y<months.length; y++)
this.when.appendChild(
this.createOption(
((month > months[y][2] || (month === months[y][2] && currentDay < day)) ? year+ "-" : "") + months[y][0], months[y][1]));
this.table.rows[2].style.display = 'table-row';
}
//Load selected again
for(var z=0; z<this.when.options.length; z++){
for(var j=0; j<selected.length; j++){
if(this.when.options[z].value === selected[j]){
this.when.options[z].selected="selected"; break;
}
}
}
//Dynamic height
this.when.size = this.when.options.length;
};
advancedDate.prototype.appendInElement = function(){
var e = document.getElementById(this.id);
e.appendChild(this.table);
};