[英]Uncaught TypeError: Cannot read property 'value' of null
This my PHP code: 这是我的PHP代码:
echo '<td>';
$r= $item['Rate'];
echo '<input id="ratetb" onkeyup="clean(ratetb)" onkeydown="clean(ratetb)" name="ratetb" autocomplete="off" style="text-align:center" type="text" value="'.$r.'" >';
echo '</td>';
and this is my JavaScript function: 这是我的JavaScript函数:
function clean(e) {
var textfield=document.getElementById(e);
var replc=/[^0-9 .]/gi;
textfield.value=textfield.value.replace(replc,"");
}
when executes this i got error 执行此操作时出现错误
Uncaught TypeError: Cannot read property 'value' of null
未捕获的TypeError:无法读取null的属性“值”
Please check call function and be sure there is an item with id e because error says that textfield is null so cant be value of null object. 请检查调用函数,并确保有一个ID为e的项,因为错误表明文本字段为null,因此不能为null对象的值。
Edit: After change your question it must be onkeyup="clean('ratetb')"
编辑:更改您的问题后,它必须是
onkeyup="clean('ratetb')"
you are not passing in a string 你没有传递字符串
echo '<input id="ratetb" onkeyup="clean(ratetb)" onkeydown="clean(ratetb)"
should be 应该
echo '<input id="ratetb" onkeyup="clean(\'ratetb\')" onkeydown="clean(\'ratetb\')"
better yet, pass in the object reference 更好的是,传递对象引用
echo '<input id="ratetb" onkeyup="clean(this)" onkeydown="clean(this)"
and change the function 并更改功能
function clean(textfield) {
var replc=/[^0-9 .]/gi;
textfield.value=textfield.value.replace(replc,"");
}
Or even better, do not use inline events. 甚至更好的是,不要使用内联事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.