简体   繁体   English

下拉菜单和单选按钮未通过验证

[英]Drop down and radio button not getting validate

This is a link to a working form : http://jsbin.com/vuroyaparo/2/ 这是工作表格的链接: http : //jsbin.com/vuroyaparo/2/

I am trying to validate the form on click of any of the three buttons (radiobutton, 4 estimated daily use button, Save Product) 我试图通过单击三个按钮(单选按钮,4个估计的日常使用按钮,保存产品)中的任何一个来验证表单

a) If the user clicks on any of the buttons give an alert that shows all the fields that are empty and keep updating the errors as per input. a)如果用户单击任何按钮,将发出警报,显示所有为空的字段,并根据输入不断更新错误。

As I put in my function returnVal in the code i get Errors in console 当我在代码中放入函数returnVal时,控制台出现错误
Uncaught TypeError: Cannot read property 'catDropdown' of null 未捕获的TypeError:无法读取null的属性'catDropdown'

It runs my function at the start but does not validate even by 1st drop down. 它从一开始就运行我的功能,但即使第一个下拉菜单也无法验证。 catDropdown is a variable that tags the HTML select element. catDropdown是标记HTML select元素的变量。

For checkbox same issue so I attempted to write 2 options (commented at the end of the js file) 对于复选框相同的问题,因此我尝试编写2个选项(在js文件末尾评论)

I am missing something. 我想念一些东西。 (either big or small) = Ahhh; (无论大小)=啊;

Can some one help me please. 有人能帮助我吗。 My first attempt at validation. 我第一次尝试验证。 I am looking at JS solution only since I dont know Jquery. 我只看JS解决方案,因为我不了解Jquery。

Thanks 谢谢

The error Uncaught TypeError: Cannot read property 'catDropdown' of null arises from the fact that you call var energyForm = document.getElementById("energyForm"); 错误Uncaught TypeError:无法读取null的属性'catDropdown'是由于您调用var energyForm = document.getElementById("energyForm"); yet you have no element in your HTML having ID='energyForm'. 但您的HTML中没有ID ='energyForm'的元素。 You can give the form an ID and reference that or, call document.getElementsByName("energyForm")[0] . 您可以给表单一个ID并引用它,或者调用document.getElementsByName("energyForm")[0]

Also notice that the line if(energyForm.catDropdown.selectedIndex = 0){ would be assigning the value 0 to energyForm.catDropdown.selectedIndex, not comparing its value to 0. This aside, you likely just want this line to be: if(catDropdown.selectedIndex == 0){ 还要注意, if(energyForm.catDropdown.selectedIndex = 0){行将为EnergyForm.catDropdown.selectedIndex分配值0,而不是将其值与0进行比较。此外,您可能只希望此行为: if(catDropdown.selectedIndex == 0){

Further notice that the line var catDropdown = document.getElementById("dd1"); 进一步注意,该行var catDropdown = document.getElementById("dd1"); will also return null since you have no element in your HTML with ID='dd1'. 由于您的HTML中没有ID ='dd1'的元素,因此也会返回null。 The select with ID='ddl' and name='dd1' can be obtained by using var catDropdown = document.getElementById("ddl"); 可以通过使用 var catDropdown = document.getElementById("ddl"); 获得ID ='ddl'和名称='dd1'的选择 var catDropdown = document.getElementById("ddl"); or var catDropdown = document.getElementById("dd1")[0]; var catDropdown = document.getElementById("dd1")[0];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM