尝试关注for循环中选择列表的第一个对象。循环使用相同的.txt填充列表,以使多个相同的选择框
<script type="text/javascript">
for(i = 0; i < 10; i++){
var names = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
document.write('<tr>');
document.write('<td>');
document.write('Enter name '+ names[i]);
document.write('</td>');
document.write('<td>');
document.write('<select id=' + names[i] + '></select>');
var textFile = "/names.txt";
jQuery.get(textFile, function(textFileData) {
var EachName= textFileData.split("\n");
for (q = 0; q < 10; q++) {
var select = document.getElementById(names[q]);
for (var j = 0, len = EachName.length; j < len; j++) {
var option = document.createElement('option');
option.text = option.value = EachName[j];
select.add(option, 0);
};
};
});
$(document).ready(function(){
$("select:first").focus();
});
document.write('</td>');
document.write('</tr>');
};
</script>
我正在尝试关注列表中的第一个选择对象,但无论我在哪里放置JavaScript,它都不会集中注意力。我不知道我做错了什么。有什么想法吗?
答案 0 :(得分:0)
尝试这样的事情:
$(document).ready(function(){
for(i = 0; i < 10; i++){
var names = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
document.write('<tr>');
document.write('<td>');
document.write('Enter name '+ names[i]);
document.write('</td>');
document.write('<td>');
document.write('<select id=' + names[i] + '></select>');
var textFile = "/names.txt";
jQuery.get(textFile, function(textFileData) {
var EachName= textFileData.split("\n");
for (q = 0; q < 10; q++) {
var select = document.getElementById(names[q]);
for (var j = 0, len = EachName.length; j < len; j++) {
var option = document.createElement('option');
option.text = option.value = EachName[j];
select.add(option, 0);
}
}
});
document.write('</td>');
document.write('</tr>');
}
document.getElementById("1").selectedIndex = 0;
document.getElementById("1").focus();
});
将您的逻辑用于填充document.ready
函数中的选择...