Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用javascript检查表单中的字段是否可用_Javascript_Html_Forms - Fatal编程技术网

如何使用javascript检查表单中的字段是否可用

如何使用javascript检查表单中的字段是否可用,javascript,html,forms,Javascript,Html,Forms,我动态创建文本元素,表单中的所有可用字段都不是强制性的,但是如何使用javascript检查字段是否可用,我尝试了以下方法 if (!document.forms[0].dynamicField1 === undefined) { alert("dynamicField1"); } 如果我尝试上面的代码,我会得到以下异常 SCRIPT5007: Unable to get value of the property 'type

我动态创建
文本
元素,表单中的所有可用字段都不是强制性的,但是如何使用
javascript
检查字段是否可用,我尝试了以下方法

if (!document.forms[0].dynamicField1 === undefined) {
                   alert("dynamicField1");
            }   
如果我尝试上面的代码,我会得到以下异常

SCRIPT5007: Unable to get value of the property 'type': object is null or undefined

谢谢

您需要请求typeof,如果它不存在,将返回undefined

if (typeof document.forms[0].dynamicField1 === "undefined") {
   alert("dynamicField1");
} 
例如:


有一点很重要,那就是您需要有
标记才能使验证生效。如果您没有
脚本将失败。

这些字段是什么样子的?您确定是代码导致了错误吗?