0

嗨,我有一个表单,它使用 javascript 在按钮单击时动态生成表格行,一切正常,但现在我想将焦点添加到表格行中新生成的输入框上,所以有人可以帮忙吗?这是我的脚本

            <script language="javascript" type="text/javascript">
                var jj=1;
                function addRow()
                {
                    //alert(jj)
                    var tbl = document.getElementById('zimtable');
                    var lastRow = tbl.rows.length;
                    var iteration = lastRow - 1;
                    var row = tbl.insertRow(lastRow);

                    var firstCell = row.insertCell(0);
                    var el = document.createElement('input');
                    el.type = 'text';
                    el.name = 'zimname_' + jj;
                    el.id = 'zimname_' + jj;
                    el.size = 40;
                    el.maxlength = 40;
                    firstCell.appendChild(el);



                    var secondCell = row.insertCell(1);
                    var el2 = document.createElement('input');
                    el2.type = 'text';
                    el2.name = 'zimmob_' + jj;
                    el2.id = 'zimmob_' + jj;
                    el2.size = 10;
                    el2.maxlength = 10;
                    secondCell.appendChild(el2);

                    var thirdCell = row.insertCell(2);
                    var element4 = document.createElement("select");
                    element4.name ='zim_'+jj;
                    var option1 = document.createElement("option");
                    option1.value='TRUSTY';
                    option1.innerHTML='TRUSTY';
                    element4.appendChild(option1);

                    var option2 = document.createElement("option");
                    option2.value='MUQAMI HAZRAT';
                    option2.innerHTML='MUQAMI HAZRAT';              
                    element4.appendChild(option2);

                    var option3 = document.createElement("option");
                    option3.value='MASJIDWAR JAMAAT KA SAATHI';
                    option3.innerHTML='MASJIDWAR JAMAAT KA SAATHI';             
                    element4.appendChild(option3);

                    thirdCell.appendChild(element4);

                    var fourthCell = row.insertCell(3);
                    var el3 = document.createElement('input');
                    el3.type = 'text';
                    el3.name = 'zemail_' + jj;
                    el3.id = 'zemail_' + jj;
                    el3.size = 40;
                    el3.maxlength = 40;
                    fourthCell.appendChild(el3);

                    firstCell.focus();

                    // alert(i);
                    jj++;

                    makhtab.hh.value=jj;
                    //  alert(jj);
                }
            </script>

我想在生成的表格行中的第一个输入框上添加焦点

4

2 回答 2

2

换行:

firstCell.focus();

至:

el.focus();
于 2013-05-06T07:11:07.860 回答
0

尝试这个

$('#zimname_1').focus();
于 2013-05-06T07:10:40.357 回答