how to assign variable value after window onload
How to show the textboxes.....see. The max value I will assign after onload so following code did not work........
max value is equal to the user selected files count. I cant assign before onload the page..
<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>
<divs>
<noscript>Please enable JavaScript to see this form correctly</noscript>
</divs>
<div id="filename"></div>
<p>
<input type="submit" name="submit" value="Store in database" />
</p>
</form>
<!-------------user selected file's name in array------------------>
var filelist=new Array();
function insert(filess)
{
filelist[filelist.length]=filess;
var sahan=(filelist.valueOf());
}
max=filelist.length;
window.onload = function() {
v开发者_运维知识库ar form = document.forms[0];
var container = form.getElementsByTagName('divs')[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="text" name="topic' + i + '" /></fieldset>';
goto(form, 0);
}
There are too many issues to mention.
- you need to get the maximum before you use it, so call insert and from there issue the call to build the list
- you need to either pass the maximum around OR make it global in scope
- you are using two different ajax methods
- you are not using jQuery everywhere and so on.
Here is a working demo when maximum is set before onload.
http://jsfiddle.net/mplungjan/rnJWX/
var maximum=0, current = 0;
$(document).ready(function(){
maximum=10;
var container = $("#sankar");
container.hide();
container.empty();
for (var i = 0; i < maximum; i++) {
container.append('<fieldset style="width: 250px; height: 80px;"><legend>Files of ' +(i + 1) + ' / ' + maximum + '</legend><input type="text" name="name' + i + '" /><br /><br /><input type="text" name="topic' + i + '" /></fieldset>');
}
goto(form, 0);
container.show();
})
function goto(form, pos) {
current += pos;
$('#my_div').html("Hiding all "+maximum+" sets and showing set #"+current+"..."); // sankar[curent];
form.prev.disabled = current <= 0;
form.next.disabled = current >= maximum - 1;
// you can do jQuery here too
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();
}
Well, since you've given your "container" element an ID, you should use document.getElementById('divs')
instead of form.getElementsByTagName('divs')[0]
.
In your current code, container
would be undefined
.
精彩评论