我正在为我的教堂制作一个动态表格,在我尝试获取动态选择的选定索引的值之前我没有任何问题。选择由 JavaScript DOM 生成。下面是生成选择的代码块以及尝试访问、检索值的测试函数。我真的很感激你能提供的任何帮助。我浏览了我的代码书库,在网上搜索了几天,找不到任何可以解决问题的东西。我可以从动态文本框中检索用户输入,但无法从选择中获取任何内容。
感谢您的时间和帮助。如果您想查看完整的源代码Full JavaScript Code
/ * ** * ** * ** * ** * ** * ** * ** * ** /
var t_shirt = 0;
for(var i = 0; i < 1; i++){
var cell = document.createElement('td');
var t_shirt_size = document.createElement('select');
t_shirt_size.setAttribute('name','t_shirt_size');
t_shirt_size.setAttribute('id','t_shirt'+t_shirt);
t_shirt_size.setAttribute('class','dynamic_select_shirt');
var topt0 = document.createElement('option');
topt0.setAttribute('id','option0');
topt0.setAttribute('value','T-Shirt Size');
topt0.innerHTML = 'T-Shirt Size';
var topt1 = document.createElement('option');
topt1.setAttribute('id','option1');
topt1.setAttribute('value','Youth Small');
topt1.innerHTML = 'Youth Small';
var topt2 = document.createElement('option');
topt2.setAttribute('id','option2');
topt2.setAttribute('value','Youth Medium');
topt2.innerHTML = 'Youth Medium';
var topt3 = document.createElement('option');
topt3.setAttribute('id','option3');
topt3.setAttribute('value','Youth Large');
topt3.innerHTML = 'Youth Large';
var topt4 = document.createElement('option');
topt4.setAttribute('id','option4');
topt4.setAttribute('value','Youth X-Large');
topt4.innerHTML = 'Youth X-Large';
var topt5 = document.createElement('option');
topt5.setAttribute('id','option5');
topt5.setAttribute('value','Adult Small');
topt5.innerHTML = 'Adult Small';
var topt6 = document.createElement('option');
topt6.setAttribute('id','option6');
topt6.setAttribute('value','Adult Medium');
topt6.innerHTML = 'Adult Medium';
var topt7 = document.createElement('option');
topt7.setAttribute('id','option7');
topt7.setAttribute('value','Adult Large');
topt7.innerHTML = 'Adult Large';
var topt8 = document.createElement('option');
topt8.setAttribute('id','option8');
topt8.setAttribute('value','Adult X-Large');
topt8.innerHTML = 'Adult X-Large';
t_shirt++;
//alert('made it past ++ topt ');
t_shirt_size.appendChild(topt0);
t_shirt_size.appendChild(topt1);
t_shirt_size.appendChild(topt2);
t_shirt_size.appendChild(topt3);
t_shirt_size.appendChild(topt4);
t_shirt_size.appendChild(topt5);
t_shirt_size.appendChild(topt6);
t_shirt_size.appendChild(topt7);
t_shirt_size.appendChild(topt8);
cell.appendChild(t_shirt_size);
row.appendChild(cell);
}
table_body.appendChild(row);
}
table_nop.appendChild(table_body);
mul.appendChild(table_nop);
table_nop.setAttribute('id','dynamic_table_main');
document.getElementById('form_div').style.display='block';
/ * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * / 代码我试图用它来检索值。/ * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * ** * *** /
function a(){
var cname = document.getElementById('tbNAME'+0).value;
var cbd = document.getElementById('tbBD'+0).value;
var cAORD = document.getElementById('tbAORD'+0).value;
var cgender = document.getElementById('gender_select'+0).selectedIndex;
var cshirt = document.getElementById('t_shirt'+0);
alert(cshirt.length);
}
function check_dynamic_table(){
var errorc = 0;
var nop = check_id_global;
for(var i =0; i < nop;i++){
var cname = document.getElementById('tbNAME'+i).value;
var cbd = document.getElementById('tbBD'+i).value;
var cAORD = document.getElementById('tbAORD'+i).value;
var cgender = document.getElementById('gender_select'+i).value;
var cshirt = document.getElementById('t_shirt'+i).value;
if(cname == "" || cname == null){
alert('Please Fill Out All Fields:' +cname );
}
if(cbd == "" || cbd == null){
alert('Please Fill Out All Fields:'+cbd);
}
if(cAORD == "" || cAORD == null){
alert('Please Fill Out All Fields:'+cAORD);
}
if(cgender == 'gender'){
alert('Please make a Gender Selection:'+cgender);
}
if(cshirt == 'T-Shirt Size'){
alert('Please make a T-Shirt Selection:'+cshirt);
}
}
}