0
我試圖建立與JavaScript的一個選擇列表的:填寫一個選項的文本/值用JavaScript
<script type='text/javascript'>
var prg =1;
function add_texte() {
\t var objTo = document.getElementById('sousTexte'+str);
\t var divtest = document.createElement("div");
\t divtest.innerHTML ='<div">'
\t \t \t +'<select name="choixType'+ prg +'" id="choixType'+ prg +'">'
\t \t \t +'<option value="" selected="selected">--choose--</option>'
\t \t \t +'</select>'
+'<div id="sousTexte'+prg+'"></div>';
objTo.appendChild(divtest);
for (i=0; i<10; i++)
\t {
formulaire.choixType+i+.options.length= 10;
formulaire.choixType+i+.options[i].value =i;
formulaire.choixType+i+.options[i].text =i;
}
prg++;
}
</script>
<div>
<select name="choixType0" id="choixType0" >
<option value="" selected="selected">---choose-</option>
</select>
<button type="button" id="btn_add_texte" onclick="add_texte();">
</button>
</div>
\t \t \t \t \t \t \t \t \t
<div id="sousTexte0">
</div>
我可以填寫列表,如果變量i是固定的:例如,如果我使用方法:
formulaire.choixType1.options[i].value =i;
or
formulaire.choixType2.options[i].value =i;
如何添加變量i與choixType? 感謝您的幫助
是這就是我想要的,但類型必須是2,但是當我使用formulaire.choixType [2] .options [i] .value = i;它的工作原理和當我使用formulaire.choixType [type] .options [i] .value = i;不工作 – oaninat
你以前是否定義過'var type =「your_type」'?否則,你需要將它稱爲'formulaire.choixType [「your_type」]。options [i] .value = i;' – RaV
是的,我做了,這是錯誤:'未捕獲TypeError:無法讀取屬性'2'未定義',(謝謝你RAV的幫助) – oaninat