Динамически генерируемые переключатели делают флажок = true в Javascript - PullRequest
1 голос
/ 22 июля 2011

У меня динамически генерируются переключатели на основе массива.Имя и идентификатор переключателей автоматически увеличиваются с 1.

Проблема в том, что я не могу получить доступ к их идентификатору или имени внутри цикла for.

var t;
for(var i=0; i<k.length; i++) //k is the array of values selected.
{
    t = "t" + i;
    // 'i' is the auto incremented radio button name and id
    if (document.form1.getElementById(t).value == k[i]) 
    {
        // need 'i' to auto increment up to match radio button name and id
        //document.form1.i.checked = true; 
        document.form1.getElementById(t) = true;
    }
}

Переключатели, построенные через XSL:

<xsl:for-each select="a/b">
    <input id="t{autoincnum}" type="radio" name="t{autoincnum}" value="{c[@value='1']/@value}"/>
    <input id="t{autoincnum}" type="radio" name="t{autoincnum}" value="{c[@value='2']/@value}"/>
    <input id="t{autoincnum}" type="radio" name="t{autoincnum}" value="{c[@value='3']/@value}"/>
</xsl:for-each>

Ответы [ 2 ]

1 голос
/ 22 июля 2011

Попробуйте это

for(var i=0; i<k.length; i++) //k is the array of values selected.
{
  // 'i' is the auto incremented radio button name and id
if (document.getElementById("rb_"+i).value == radiobuttonname[i]) 
{
  // need 'i' to auto increment up to match radio button name and id
    document.getElementById("rb_"+i).checked = true; 

}

}
1 голос
/ 22 июля 2011

попробуйте использовать имя, которое содержит (и начинается) с буквы:

for(var i=0; i<k.length; i++)
{
  var btnName = "btn"+i;
  var btn = document.getElementById(btnName);
  // do sth with button ...
}
...