[英]IE11 Unable to get property 'value' of undefined or null reference
it's seems that I'm having a frustrating problem and can't seem to find an answer. 看来我遇到了一个令人沮丧的问题,似乎找不到答案。 I'm trying to get the value of the element in <td>
tag. 我正在尝试获取<td>
标记中元素的值。 The id reaches the function but for some reason I can't get the value of it. id到达函数,但由于某种原因我无法获取它的值。
JS JS
function f(id)
{
console.log(id);
expr=/ /gi;
value = document.getElementById(id).value;
value = value.replace(expr, "");
//remaining code
}
PHP 的PHP
print "<td style=\"height:20px;\"><input $disbled type=\"text\" name=\"".$values[$i][0]."\" onChange=\"return f('".$values[$i][0]."')\" value=\"".$values[$i][1]."\" class=\"".$values[$i][2]."\"></td>\n";
Any help would be appreciated! 任何帮助,将不胜感激!
A couple of problems there: 那里有两个问题:
Your element doesn't have an id
at all 您的元素根本没有id
Your code is falling prey to The Horror of Implicit Globals (that's a post on my anemic little blog) . 您的代码成为“隐式全球性恐怖”的牺牲品(这是我贫乏的小博客上的帖子) 。
Here's a fixed version: 这是固定版本:
function f(name)
{
console.log(name);
var expr=/ /gi;
var value = document.querySelector('[name="' + name + '"]').value;
value = value.replace(expr, "");
//remaining code
}
#1 is fixed by using querySelector
and an attribute selector selecting by name
通过使用querySelector
和按name
选择的属性选择器来固定#1
#2 is fixed by declaring the local variables in f
#2通过在f
声明局部变量来固定
You could also fix #1 by giving your element an id
and sticking with getElementById
. 您还可以通过给元素一个id
并坚持使用getElementById
来修复#1。
The input you are trying to access has not ID property defined. 您尝试访问的输入未定义ID属性。 You shall add it in order to access the input object via getElementById(). 您应该添加它以便通过getElementById()访问输入对象。
<input $disbled id=\"".$id."\" type=\"text\" name=\"".$values[$i][0]."\" onChange=\"return f('".$values[$i][0]."')\" value=\"".$values[$i][1]."\" class=\"".$values[$i][2]."\">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.