2016-07-27 14 views
0

Ich versuche, eine Select mit Javascript zu bauen:eine Option Text/Wert mit JavaScript füllen

<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>

ich die Liste füllen kann, wenn die Variable i festgelegt ist: zum Beispiel, wenn i Verwenden Sie:

formulaire.choixType1.options[i].value =i; 
or 
formulaire.choixType2.options[i].value =i; 

Wie kann ich die Variable i mit ChoixType hinzufügen? danke für jede Hilfe

Antwort

0

<select name="choixType['+ prg +']" id="choixType'+ prg +'">

var type = 'type2'; 
formulaire.choixType[type].options[i].value =i; 

Ist es das, was Sie suchen?

+0

Ja, das ist was ich will, aber Typ muss 2 sein, aber wenn ich formulaire.choixType [2] .options [i] .value = i; es funktioniert und wenn ich formulaire.choixType [type] .options [i] .value = i; funktioniert nicht – oaninat

+0

Haben Sie schon 'var type =" your_type "' definiert? Andernfalls müssen Sie es wie 'formulaire.choixType [" Ihr_Typ "] nennen. Options [i] .value = i;' – RaV

+0

ja ich tat, und das ist der Fehler: 'Uncaught TypeError: Kann Eigenschaft' 2 'von undefined nicht lesen ', (danke RAV für deine Hilfe) – oaninat

Verwandte Themen