how to show text box in div
This is my index.php
page. Also the form is in my popup box for get the description.
<form action="" method="post" onsubmit="store(this); return false">
<p>
<input type="button" name="prev" onclick="goto(this.form, -1)" value="Previous" />
<input type="button" name="next" onclick="goto(this.form, +1)" value="Next" />
</p>
<div>
<noscript>Please enable JavaScript to see this form correctly</noscript>
</div>
<p>
<input type="submit" name="submit" value="Store in database" />
</p>
this is my Javascript file upload.js
.
var files=new Array(); function insert(fileslist) { files[files.length]=fileslist;
var filenames=(files.valueOf());
var max = files.length; var current = 0; function goto(form, pos) {
current += pos;
form.prev.disabled = current <= 0;
form.next.disabled = current >= max - 1;
var fields = form.getElementsByTagName('fieldset');
for (var i = 0; i < fields.length; i++) fields[i].style.display = 'none';
fields[current].style.display = 'block';
form['name' + current].focus();
}
function store(form) {
var input = form.getElementsByTagName('input');
var data = '';
for (var i = 0; i < input.length; i++) {
if (input[i].getAttribute('type') == 'text')
data += '&' + input[i].getAttribute('name') + '=' + input[i].value;
}
data = encodeURI('n=' + max + data);
var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
xhr.open('POST', 'datainsert.php', true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.setRequestHeader('Content-length', data.length);
xhr.setRequestHeader('Connection', 'close');
xhr.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
if (this.responseText != '')
alert(this.responseText);
else {
form.submit.value = 'Saved!';
setTimeout(function() { form.submit.value = 'Save to database' }, 500);
}
}
}
xhr.send(data);
}
window.onload = function()
{
var form = document.forms[0];
var container = form.getElementsByTagName('div')[0];
container.innerHTML = '';
for (var i = 0; i < max; i++)
container.innerHTML += '<fieldset style="width: 250px; height: 80px;"><legend>Files of ' +(i + 1) + ' / ' + max + '</legend><input type="text" name="name' + i + '" /><br /><br /><input type=开发者_JAVA百科"text" name="topic' + i + '" /></fieldset>';
goto(form, 0);
}
The <div>…</div>
did not show the text boxes. Why?
My gut is telling me that you are passing max
incorrectly, so it is either a undefined or 0 so loop is never run, OR your selectors are wrong - add IDs to your elements and do it like that:
<form id="myForm" action="" method="post" onsubmit="store(this); return false">
<p>
<input type="button" name="prev" onclick="goto(this.form, -1)" value="Previous" />
<input type="button" name="next" onclick="goto(this.form, +1)" value="Next" />
</p>
<div id="container">
<noscript>Please enable JavaScript to see this form correctly</noscript>
</div>
<p>
<input type="submit" name="submit" value="Store in database" />
</p>
window.onload = function()
{
var form = document.getElementById('myForm');
var container = document.getElementById('container');
container.innerHTML = '';
var html = '';
for (var i = 0; i < max; i++)
{
var ofValue = (i + 1) + ' / ' + max;
html += '<fieldset style="width: 250px; height: 80px;">';
html += '<legend>Files of ' + ofValue + '</legend>';
html += '<input type="text" name="name' + i + '" /><br /><br />';
html += '<input type="text" name="topic' + i + '" /></fieldset>';
}
container.innerHTML = html;
goto(form, 0); //I am assuming this is a valid, defined method
//because it is also called from onclicks of Next/Prev buttons
}
put id="div1"
in you div tag,
this will behave as identifier for JavaScript in you whole page
<div id="dvi1">
<noscript>Please enable JavaScript to see this form correctly</noscript>
</div>
<form action="" method="post" onsubmit="store(this); return false">
<p>
<input type="button" name="prev" onclick="goto(this.form, -1)" value="Previous" />
<input type="button" name="next" onclick="goto(this.form, +1)" value="Next" />
</p>
<div id="div1">
<noscript>Please enable JavaScript to see this form correctly</noscript>
</div>
<p>
<input type="submit" name="submit" value="Store in database" />
</p>
</form>
window.onload = function()
{
var container = documentgetElementByid("div1");
container.innerHTML = '';
for (var i = 0; i < max; i++)
container.innerHTML += '<fieldset style="width: 250px; height: 80px;"><legend>Files of ' +(i + 1) + ' / ' + max + '</legend><input type="text" name="name' + i + '" /><br /><br /><input type="text" name="topic' + i + '" /></fieldset>';
goto(form, 0);
}
精彩评论