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