简体   繁体   English

未捕获的TypeError:无法读取null的属性“值”

[英]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.

相关问题 未捕获的类型错误:无法读取 null 的属性“值” - Uncaught TypeError: Cannot read property 'value' of null 未捕获的类型错误:无法读取 null 的属性“值” - Uncaught TypeError: Cannot read property 'value' of null 未捕获的TypeError:值更改时无法读取null的属性“值” - Uncaught TypeError: Cannot read property 'value' of null at value change 未捕获(承诺)类型错误:无法读取 null 的属性“值” - Uncaught (in promise) TypeError: Cannot read property 'value' of null JAVASCRIPT-未捕获的TypeError:无法读取null的属性“值” - JAVASCRIPT - Uncaught TypeError: Cannot read property 'value' of null 如何解决“未捕获的TypeError:无法读取null的属性&#39;value&#39;” - How to solved “Uncaught TypeError: Cannot read property 'value' of null” 未捕获的TypeError:无法以离子反应形式读取null的属性“值” - Uncaught TypeError: Cannot read property 'value' of null in ionic reactive form 如何修复未捕获的类型错误:无法读取 null 的属性“值” - how to fix the Uncaught TypeError: Cannot read property 'Value' of null 未捕获的类型错误无法读取 javascript 中 null 的属性(读取“值”) - Uncaught typeerror cannot read property of null (reading 'value') in javascript onclick提交按钮:未捕获的TypeError:无法读取null的属性“值” - onclick submit Button: Uncaught TypeError: Cannot read property 'value' of null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM