0
我有预订座位领域为阵,下面检查空字段的JavaScript
<input type="checkbox" name="book[]" value="<?php echo $i; ?> /><?php echo $i; ?>
<input type="submit" value="BOOK" onclick = "checkIfSeatEmpty()" />
我用来验证JavaScript代码一个简单的HTML文件,如果该字段book
是空的是:
var x=document.forms["bkfrm"].getElementById("chkbx").value;
//alert(x); alerts 'undefined' !
// var x=document.forms["bkfrm"].getElementById("book").length;
alert(x);
if (x==null || x=="")
{
alert("First name must be filled out");
return false;
}
return TRUE;
但上述代码提示为undefined
!我如何检查该字段是否为空?
你的代码中没有这样的id'chkbx'。 – botzko 2012-03-07 12:18:12
对不起'书' – AssamGuy 2012-03-07 12:21:36
只需添加id =“book”。在你的代码中只有名称属性,但没有id属性。通过id获取getElementById搜索元素。 – botzko 2012-03-07 12:23:45