Javascript错误:未捕获TypeError:无法读取null的属性'value'[duplicate](Javascript error: Uncaught TypeError: Cannot read property 'value' of null [duplicate])

这个问题在这里已有答案:

我有两个问题。
1)我写了这个html标签,并在单独的文件中创建了javascript。

<input type="text" id="callno" value="3333">

在js中,我有

var PhoneNumber = document.getElementById("callno").value;

但是,当我在浏览器(谷歌浏览器)中查看控制台时,我发现有一个错误未捕获的TypeError:无法读取null的属性'value'

2)每当我更新html标签的值时,如何自动更新PhoneNumber的值,我怎么能这样做。


This question already has an answer here:

I have two questions.
1) I wrote this html tag and created javascript in separate file.

<input type="text" id="callno" value="3333">

In the js, I have

var PhoneNumber = document.getElementById("callno").value;

However, when I look at the console in the browser (Google Chrome) I saw that there is an error Uncaught TypeError: Cannot read property 'value' of null.

2) How can I make that each time when I update value of html tag in above the value of PhoneNumber updated automatically.

2023-03-27 20:03

满意答案

这应该工作:

<!DOCTYPE html>
<html>
<body>

<input id="myInput" onchange="myFunction()" value="1234">

<p>Change the above input value and click enter.</p>


<p id="myOutput">VALUE HERE</p>

<script>

var x;

function myFunction() {
    x = document.getElementById("myInput").value;
    document.getElementById("myOutput").innerHTML = x;
}
</script>

</body>
</html>


This should work:

<!DOCTYPE html>
<html>
<body>

<input id="myInput" onchange="myFunction()" value="1234">

<p>Change the above input value and click enter.</p>


<p id="myOutput">VALUE HERE</p>

<script>

var x;

function myFunction() {
    x = document.getElementById("myInput").value;
    document.getElementById("myOutput").innerHTML = x;
}
</script>

</body>
</html>

相关问答

更多

JAVASCRIPT - 未捕获的TypeError:无法读取null的属性“value”(JAVASCRIPT - Uncaught TypeError: Cannot read property 'value' of null)

编辑: 有点不同的实现: http : //jsfiddle.net/SfTR2/确保在HTML之后加载JS,或使用window.onload 原始答案: 您需要在验证函数中获取长度: function validatePhone() { var phonenum = document.getElementById("dvr").value.length; //MOVE IT HERE if (phonenum == 10){ docu...

未捕获的TypeError:在else语句中无法读取null的属性'style'(Uncaught TypeError: Cannot read property 'style' of null in else statement)

使用Ref执行DOM操作 HTML: <div id="error" style = {{display = 'none'}} ref= { (node)=>{ mynode = node }}> 或以课程为基础 <div id="error" style = {{display = 'none'}} ref= { (node)=>{ this.mynode = node }}> JS: if(count > parseInt(this.credentials.noOfusers))...

谷歌地图错误未捕获TypeError:无法读取null的属性'offsetWidth'(google maps error Uncaught TypeError: Cannot read property 'offsetWidth' of null)

document.getElementById(ele)不需要哈希符号,我不这么认为。 尝试 createMap(qc_1,"text Message", "1_map"); 不 createMap(qc_1,"text Message", "#1_map"); document.getElementById(ele) doesn't need the hash symbol, I don't think. try createMap(qc_1,"text Message", "1_map"); ...

未捕获的TypeError:无法读取null的属性'getContext'(Uncaught TypeError: Cannot read property 'getContext' of null)

因为您尚未正确关闭playerCanvas的画布标记: <canvas id="backgroundCanvas" width="550" height=600"></canvas> <canvas id="playerCanvas" width="550" height=600"></canvas> <canvas id="enemiesCanvas" width="550" height=600"></canvas> Because you have not closed the canva...

Extjs5 Uncaught TypeError:无法读取null的属性'metaData'(Extjs5 Uncaught TypeError: Cannot read property 'metaData' of null)

Ext.define(TreeSearchProxy, { extend: 'Ext.data.proxy.Ajax', alias: 'proxy.treesearchproxy', requires: [ TreeSearchResultsReader ], reader: 'treesearchreader', url : eaxies.util.Globals.ServletName, simpleSortMode : true, extraParams :{ ActionID: eaxi...

未捕获的TypeError:无法使用数组读取null的属性“data”(Uncaught TypeError: Cannot read property 'data' of null using array)

if (typeof(this.callbacks['changed']) !== 'undefined') { if (obj && obj.item && typeof(obj['item']['data']) !== 'undefined' && (typeof(obj['item']['data'] != null))) { this.callbacks['changed'](row, obj['item']['data']); } ...

Javascript错误:未捕获TypeError:无法读取null的属性'value'[duplicate](Javascript error: Uncaught TypeError: Cannot read property 'value' of null [duplicate])

这应该工作: <!DOCTYPE html> <html> <body> <input id="myInput" onchange="myFunction()" value="1234"> <p>Change the above input value and click enter.</p> <p id="myOutput">VALUE HERE</p> <script> var x; function myFunction() { x = ...

未捕获的TypeError:无法读取null的属性'documentElement'(Uncaught TypeError: Cannot read property 'documentElement' of null)

你很可能有另一个工具提示库仍在使用中(如JQueryUI / Tooltip),或者你试图直接附加工具提示到文档元素。 请仔细研究您的代码: $(document).tooltip 或类似的东西,并删除它,以使事情重新工作。 请务必检查您可能已拥有的每一个.tooltip()调用,因为它们很可能不再工作了:您需要将它们手动移植到Bootstrap以使其再次运行。 如果您想保留Bootstrap / Tooltip和JQueryUI / Tooltip (假设这是您的场景),则可以使用$ .wid...

JavaScript:未捕获的TypeError:无法读取null的属性'style'[重复](JavaScript: Uncaught TypeError: Cannot read property 'style' of null [duplicate])

问题是你在加载实际的HTML内容之前调用了hidediv()函数。 将脚本放在文档的末尾(在关闭body标签之前)或将其设置在window.onload / on document ready上 The problem is that you are calling the hidediv() function before loading the actual HTML content. Either put the script in the end of the document (befo...

未捕获的TypeError:无法在React类中读取null的属性'props'[重复](Uncaught TypeError: Cannot read property 'props' of null in React Class [duplicate])

您需要将方法绑定到构造函数中的组件实例,如此 export default class Comments extends React.Component { constructor() { super(); this.renderComment = this.renderComment.bind(this); this.handleSubmit = this.handleSubmit.bind(this); } 如果您使用阶段2的babel,您还可以重构您的方法...

相关文章

更多

命令行运行Hbase: Session 0x0 for server null, unexpected error

又重新看了下hbase的操作,以前虽说是运行过对Hbase的操作,比如直接的建表,导入数据,或者是使用 ...

Hibernate创建sessionFactory null

请问各位: 我在使用junit进行单元测试的时候,到实例化SessionFactory的时候不成功, ...

could not find system property or JNDI

Thanks everyone!! Finally got a solution for this p ...

mybatis There is no getter for property named 'xx' in 'class java.lang.String

用mybatis查询时,传入一个字符串传参数,且进行判断时,会报 There is no get ...

List用完后需要赋null吗

从数据库查询N条记录放在List集合中,然后通过request对象返回给页面,通过循环遍历将List中 ...

Failed to read auto-increment value from storage engine错误的解决方法

在进行数据的插入时,系统提示Failed to read auto-increment value f ...

null 写“==”前和写“==”有何区别

if(null == bodyParas.getModifyData()){} 和if(bodyPar ...

Guava学习笔记:Optional优雅的使用null

  在我们学习和使用Guava的Optional之前,我们需要来了解一下Java中null。因为,只有 ...

jfreechart如何去掉为null的空白占位

在第二个区组里面,14和12之间5个为null的值,但是他也占了位置,我想不让他们占位置。就是14和1 ...

Guava Optional类详解-处理null值

abstract T get()返回所包含的实例

最新问答

更多

绝地求生、荒野行动、香肠派对 哪个更好玩???(都是吃鸡类游戏)

PC上的绝地求生,是最早也是最火的大逃杀游戏。 荒野行动是网易抄袭蓝洞绝地求生制作的手游。相似度90%,还有他一起出的终结折2,这2款正在被蓝洞告,打官司呢。 手游上的绝地求生有2部都是蓝洞授权(收钱)给腾讯开发的正版ID手游。所以跟PC上做的一模一样,蓝洞也没话说。 加上吃鸡国服也是腾讯独家代理,所以根本没有什么可说的。只要这个类型的 过于相似的,腾讯都可以借蓝洞之手起诉。打压同行是国内BAT最爱干的事嘛! 香肠派对画风虽然不一样,但核心玩法还是跟人家正版的一样的,同样也是没有被授权的。 98

如何在jQuery集合中选择第n个jQuery对象?(How to select the nth jQuery object in a jQuery collection?)

你可以使用eq : var rootElement = $('.grid').find('.box').eq(0); rootElement.find('.a'); /* Use chaining to do more work */ You can use eq: var rootElement = $('.grid').find('.box').eq(0); rootElement.find('.a'); /* Use chaining to do more work */

ASP NET使用jQuery和AJAX上传图像(ASP NET upload image with jQuery and AJAX)

您可以自己手动设置FormData键和值。 Upload 创建FormData并设置新的键/值 $("#btnUpload").on("click", function(e) { e.preventDefault(); var file = $("#imguploader").get(0).file

SQL Server XML查询中包含名称空间的位置(SQL Server XML query with namespaces in the where exist)

您可能希望使用#temp.identXml.query而不是#temp.identXml.query 。 您可以在这里阅读更多相关信息SQL Server XML exists() 我相信你也可以像这样使用它 Select #temp.identXml.value('(/*:PersonIdentity/*:MasterIndexes/*:PersonIndex/*:SourceIndex)[1]','varchar(100)') as Ident ,#temp.identXml.value(

宁夏银川永宁县望远镇哪里有修mp5的?

胜利街有家电维修,电脑城,银川商场多得很…

我想用更新的日期标记所有更新的行(I would like to mark all updated rows with the date that they have been updated)

您可以使用更新后触发的触发器来执行此操作。 给出如下表: create table your_table (id int primary key, val int, last_update datetime) 每当您更新表中的内容时,此触发器将设置last_update值。 CREATE TRIGGER trigger_name ON your_table AFTER UPDATE AS BEGIN UPDATE your_table SET your_ta

郑州会计培训班

招生的,至于时间吗,就看你自己的时间段了,你可以致电0371-63300220.他们会帮你选择一下的。离你最近,最专业的培训班。

如何定位数组中的负数,并得到所有正数的总和?(How to target e negative number from an array, and get the sum of all positive numbers?)

只需创建一个条件来检查它是正数还是负数,然后定义一个空的数组negatives ,如果数字是负数,则将其推到负数组中,如果是正数,则将其添加到sum变量中,请查看下面的工作示例。 function SummPositive( numbers ) { var negatives = []; var sum = 0; for(var i = 0; i < numbers.length; i++) { if(numbers[i] < 0) { negati

在响应图像上叠加网格(Overlay grid on responsive image)

使用两个linear-gradient s,我们可以创建两个简单的线条,然后每隔n%重复一次background-size 。 它看起来像这样: background: linear-gradient(to bottom, #000 2px, transparent 2px), linear-gradient(to right, #000 2px, transparent 2px); background-size: 10%; 两个渐变创建两条相交的线,长度为百分比,如下所示: 使用默认的b

无法让POST在Azure网站上运行(Could not get POST to work on Azure Website)

最后我找到了答案......我不得不删除尾随的斜线! 我使用了“ https://example.com/api/messages/ ”,这将自动产生GET,无论我使用PostAsync还是PostAsJsonAsync。 使用“ https://example.com/api/messages”,GET和POST似乎都运行良好! Finally I've found the answer.... I had to remove the trailing slash! I've used "ht