使用表单对象时,报错 form is undefine
先看例子
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>hello</title>
<script src="js/myjs.js"></script>
</head>
<body>
<form name="form">
<input name="password" value="password" />
<input name="name" value = "name" />
</form> </body>
</html>
导入的js文件代码为
function test(){
var test = form.name.value;
var test2 = form.password.value;
console.log(test);
console.log(test2);
}
test()
报错信息如下:

不采用动态导入,使用内联JS
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>hello</title>
</head>
<body>
<form name="form">
<input name="password" value="password" />
<input name="name" value = "name" />
</form>
<script>
function test(){ var test = form.name.value;
var test2 = form.password.value;
console.log(test);
console.log(test2);
}
test()
</script>
</body>
</html>
运行正确,结果如下:

移动内联js位置,
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>hello</title>
<script>
function test(){ var test = form.name.value;
var test2 = form.password.value;
console.log(test);
console.log(test2);
}
test()
</script>
</head>
<body>
<form name="form">
<input name="password" value="password" />
<input name="name" value = "name" />
</form>
</body>
</html>
又报错

原因:调用test()函数时,form对象还未生成,也可以这样子写
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>hello</title>
<script>
function test(){ var test = form.name.value;
var test2 = form.password.value;
console.log(test);
console.log(test2);
}
</script>
</head>
<body>
<form name="form">
<input name="password" value="password" />
<input name="name" value = "name" />
</form>
<script>
test()
</script>
</body>
</html>
使用表单对象时,报错 form is undefine的更多相关文章
- linux下, 再次遇到使用thinkphp的模板标签时,报错used undefined function \Think\Template\simplexml_load_string() 是因为没有安装 php-xml包
linux下, 使用thinkphp的模板标签,如 eq, gt, volist defined, present , empty等 标签时, 报错: used undefined function ...
- WPF加载Winform窗体时 报错:子控件不能为顶级窗体
一.wpf项目中引用WindowsFormsIntegration和System.Windows.Forms 二.Form1.Designer.cs 的 partial class Form1 设置为 ...
- 启动tomcat时,报错:IOException while loading persisted sessions: java.io.EOFException解决方法
报错原因:加载持久化session错误,tomcat加载时读取的文件是是*.ser,session序列化文件,文件的位置是tomcat\work\Catalina\localhost,找到sessio ...
- 格式化namenode时 报错 No Route to Host from node1/192.168.1.111 to node3:8485 failed on socket timeout exception: java.net.NoRouteToHostException: No route to host
// :: FATAL namenode.NameNode: Failed to start namenode. org.apache.hadoop.hdfs.qjournal.client.Quor ...
- 已解决: idea创建并部署SpringMVC项目时 报错 java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
用IDEA创建并运行SpringMVC项目时,最初发现没有Servlet包,这个问题已在上篇解决,然而当我们尝试去运行此时的SpringMVC项目时,发现仍然有错误.ClassNotFoundExce ...
- iOS url带中文下载时 报错解决方法
问题描述:下载文件时, 请求带中文的URL的资源时,比如:http://s237.sznews.com/pic/2010/11/23/e4fa5794926548ac953a8a525a23b6f2/ ...
- 项目配置 xml文件时 报错提示(The reference to entity "useSSL" must end with the ';' delimiter.)
这次在配置xml文件时,出现错误提示( The reference to entity “useSSL” must end with the ‘;’ delimiter.) 报错行为 <prop ...
- CentOS 7在执行yum操作时 报错
CentOS 7在执行yum操作时, 报错:Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=6&arch ...
- MySQL-配置参数时 报错:remove CMakeCache.txt and rerun cmake.On Debian/Ubuntu......
报错:remove CMakeCache.txt and rerun cmake.On Debian/Ubuntu...... 原因: 1.第一次配置参数时,不完整,出现错误!,(报错也会产生CMak ...
随机推荐
- 0219 springmvc-拦截器和响应增强
拦截器 拦截器分同步拦截器和异步拦截器: HandlerInterceptor 方法和执行时机 可以看DispathcerServlet的原来确定它的三个方法的执行时机: AsynHandlerInt ...
- vue必须掌握之组件通信(7种方法)
方法一:$emit / props 父组件通过props的方式向子组件传递,子组件通过$emit触发父组件中v-on绑定的自定义事件 <!--父组件--> <template> ...
- 《剑指Offer》各面试题总结
目录 前言 面试题4 二维数组的查找 面试题5:替换空格 面试题6:从尾到头打印链表 面试题7:重建二叉树 面试题8:二叉树的下一个节点 面试题9:用两个栈实现队列 面试题10:斐波那契数列 面试题1 ...
- MySQL的修改和删除数据表字段
MySQL的修改和删除数据表字段 写在前面: 数据库存在的意义:数据存储和数据管理. 数据库:行(数据),列(字段) 注意:本页是解决了列的字段问题.下一页是解决行的数据问题. 注意,所有的字段名,最 ...
- CVE-2019-0708 远程桌面代码执行漏洞复现
0x01 首先是靶机,这里的靶机是使用清水表哥提供的win7sp1的系统 漏洞环境使用VM安装Windows7 SP1模拟受害机Windows7 SP1下载链接:ed2k://|file|cn_win ...
- Tickets HDU - 1260 简单dp
#include<iostream> using namespace std; const int N=1e5; int T,n; int a[N],b[N]; int dp[N]; in ...
- AntDesign(React)学习-8 Menu使用 切换框架页内容页面
本节实现一个点击左侧menu在右侧content切换页面效果,原始代码请从UMI学习-6开始看 1.在pages下添加两个组件,User,UserRole import React from 'rea ...
- 有一个树形结构,实现一个方法getKeys(data,str),获取字符串str在data中的所有上级节点的名称
有一个树形结构,实现一个方法getKeys(data,str);获取字符串str在data中的所有上级节点的名称,例如: getKeys(data,'str1') 返回 ‘key1' getKeys( ...
- 继 “多闪”后“飞聊”再被diss?其实社交还能这么玩
近日头条低调上线了新的社交APP——飞聊,目前在AppStore社交排行榜第7位.但很多人使用了之后都觉得新产品的各个功能都让人想起其他的产品.兴趣小组让人想到豆瓣的兴趣小组,生活动态让人想到微博动态 ...
- Django | Unable to get repr for <class 'django.db.models.query.QuerySet'>
问题:在mysql中查询数据时,代码如下: skus = category.sku_set.filter(is_launched=True).order_by(sort_field) skus 取不到 ...