我已经研究了好几天,并从此处获取了几个示例,以找到一种解决方案来验证日期字段,以确保在移至下一页之前它不为空。但一直无法读取未定义的属性,我尝试使用 Joomla build validate.js ,它在这个自定义组件中不起作用,我希望在这里得到一些帮助来解决这个问题。
这是 JavaScript
nextpage:function(){
var deliveryy=document.getElementsByName("customPlugin")[0].value;
if(delivery == "" || delivery == null) {
alert("filled out All Delivery Date & Time");
return false; } else { .........
这是 onclick 按钮
<button type="button" id="productbuilder_next" class="productbuilder_pagination pbbutton btn" onclick="productbuilder.nextpage();"> Next</button>
这是我试图验证它不为空的字段的原始数据
<input id="<?php echo $class.$rand ?>" class="required <?php echo $class ?>" required="true" type="text" value="" size="<?php echo $this->params->custom_size ?>" name="customPlugin[<?php echo $viewData[0]->virtuemart_customfield_id ?>][<?php echo $this->_name?>][comment]"><?php if($this->params->custom_populate_alternate_field){?> <input type="text" id="alternate<?php echo $rand?>" size="30">
生成
<input id="vmcustom-datetime1930516000" class="required vmcustom-datetime hasDatepicker" required="true" type="text" value="" size="10" name="customPlugin[176][datetime][comment]">
最佳答案
您正在寻找 DOM 中不存在的名称。这样,当你访问属性值时就会抛出这个错误。
这里可以使用类。只需在元素中添加一个类并在函数中搜索它即可。
HTML:
<input id="vmcustom-datetime1930516000" class="required validDelivery vmcustom-datetime hasDatepicker" required="true" type="text" value="" size="10" name="customPlugin[176][datetime][comment]">
JS:
var validDelivery = document.getElementsByClassName("validDelivery")[0].value;
JSFiddle:http://jsfiddle.net/t1g7cfrb/1/
尝试一下,如果有帮助请告诉我!
关于Javascript 空字段验证 - 无法读取未定义的属性 'value',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28197793/