document.getElementById()通过分配动态变量 [英] document.getElementById() by assigning the dynamic variable

查看:293
本文介绍了document.getElementById()通过分配动态变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用该表的td中的输入字段的id在html表中动态创建行。
table正确创建新添加的行和输入框id也创建。但是当我尝试使用id获取该输入框时,它会抛出空异常。

  var cell1 = row.insertCell(1); 
var element1 = document.createElement(input);
element1.name = rId +_+ rowCount +_newscheduledate;
element1.id = rId +_+ rowCount +_newschedueldate;
cell1.appendChild(element1);

alert(rowCount +:+ rId); //这里alert提示值
var tt = document.getElementById(rId +'_'+ rowCount +'_newscheduledate');
alert(tt); // here null

例外情况如下

 错误:document.getElementById(rId +(_+ rowCount +_newscheduledate))为空
 

> element1.id = rId +_+ rowCount +_newschedueldate;

  element1.id = rId +_+ rowCount +_newscheduledate; 


am creating the row dynamically in html table using id of input field in td of that table. table created properly with newly added rows and input box ids also created.but when I try to get that input box using id it throws null exception.

var cell1 = row.insertCell(1);
var element1 = document.createElement("input");
    element1.name = rId + "_" + rowCount + "_newscheduledate";
    element1.id = rId + "_" + rowCount + "_newschedueldate";
    cell1.appendChild(element1);

     alert(rowCount+"  : "+rId);  //here alert shows the values
     var tt = document.getElementById(rId + '_' + rowCount + '_newscheduledate');
     alert(tt); // here null comes

Exception comes like this

Error: document.getElementById(rId + ("_" + rowCount + "_newscheduledate")) is null

解决方案

Change:-

element1.id = rId + "_" + rowCount + "_newschedueldate";

To

element1.id = rId + "_" + rowCount + "_newscheduledate";

这篇关于document.getElementById()通过分配动态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆