先看例子

<!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的更多相关文章

  1. linux下, 再次遇到使用thinkphp的模板标签时,报错used undefined function \Think\Template\simplexml_load_string() 是因为没有安装 php-xml包

    linux下, 使用thinkphp的模板标签,如 eq, gt, volist defined, present , empty等 标签时, 报错: used undefined function ...

  2. WPF加载Winform窗体时 报错:子控件不能为顶级窗体

    一.wpf项目中引用WindowsFormsIntegration和System.Windows.Forms 二.Form1.Designer.cs 的 partial class Form1 设置为 ...

  3. 启动tomcat时,报错:IOException while loading persisted sessions: java.io.EOFException解决方法

    报错原因:加载持久化session错误,tomcat加载时读取的文件是是*.ser,session序列化文件,文件的位置是tomcat\work\Catalina\localhost,找到sessio ...

  4. 格式化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 ...

  5. 已解决: idea创建并部署SpringMVC项目时 报错 java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

    用IDEA创建并运行SpringMVC项目时,最初发现没有Servlet包,这个问题已在上篇解决,然而当我们尝试去运行此时的SpringMVC项目时,发现仍然有错误.ClassNotFoundExce ...

  6. iOS url带中文下载时 报错解决方法

    问题描述:下载文件时, 请求带中文的URL的资源时,比如:http://s237.sznews.com/pic/2010/11/23/e4fa5794926548ac953a8a525a23b6f2/ ...

  7. 项目配置 xml文件时 报错提示(The reference to entity "useSSL" must end with the ';' delimiter.)

    这次在配置xml文件时,出现错误提示( The reference to entity “useSSL” must end with the ‘;’ delimiter.) 报错行为 <prop ...

  8. CentOS 7在执行yum操作时 报错

    CentOS 7在执行yum操作时, 报错:Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=6&arch ...

  9. MySQL-配置参数时 报错:remove CMakeCache.txt and rerun cmake.On Debian/Ubuntu......

    报错:remove CMakeCache.txt and rerun cmake.On Debian/Ubuntu...... 原因: 1.第一次配置参数时,不完整,出现错误!,(报错也会产生CMak ...

随机推荐

  1. jquery 调用点击事件

    触发 click 事件 $(selector).click()

  2. CTF之图片隐写术解题思路

    参考大佬的博客:https://blog.csdn.net/a_small_rabbit/article/details/79644078 深有感触,觉得写得比较全,因此将此篇文章记录在此方便以后用得 ...

  3. 在VMware下安装CentOS 7.6

    转载自https://blog.51cto.com/hnyuanzijian/2343716?appinstall=0a.点击左上角文件,新建虚拟机,选择典型安装,并下一步   b.选择稍后安装操作系 ...

  4. dubbo整合SSM登录案例

    基于dubbo/zookeeper/SSM的分布式工程   一.项目结构

  5. C++——指针5

    8.2 指向类的静态成员的指针 对类的静态成员的访问不依赖于对象,可以用普通的指针来指向和访问. //通过指针访问类的静态数据成员 #include <iostream> using na ...

  6. 巨杉TechDay回顾 | WARNING!您参加的数据库沙龙热度已爆表……

    自从2008年“大数据”这一概念被首次提出以来,在过去这10年中,几乎各行各业都或多或少受到了这一概念的影响.与此同时,在AI.云计算.物联网.区块链等新兴技术快速发展的今天,数据库己经成为了决定所有 ...

  7. Codeforces Round #600 (Div. 2) D。 Harmonious Graph

    #include<iostream> using namespace std ; ; int p[N]; int cnt; int find(int x) { if(p[x]!=x) p[ ...

  8. Weather

    Scientists say a lot about the problems of global warming and cooling of the Earth. Indeed, such nat ...

  9. 【网站】网站不能在QQ,微信中访问,申诉办法

    使用这个网站申诉即可 https://appeal.anquan.org/

  10. 百度地图和echarts结合实例

    1.由echart对象(bmapChart)获取百度地图对象(bdMap),echart对象(bmapChart)适用于所有的echart的操作和接口,百度地图对象(bdMap)适用于百度地图的所有接 ...