2012-03-07 99 views
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!我如何檢查該字段是否爲空?

+2

你的代碼中沒有這樣的id'chkbx'。 – botzko 2012-03-07 12:18:12

+0

對不起'書' – AssamGuy 2012-03-07 12:21:36

+0

只需添加id =「book」。在你的代碼中只有名稱屬性,但沒有id屬性。通過id獲取getElementById搜索元素。 – botzko 2012-03-07 12:23:45

回答

0

嘗試像下面

var x=document.forms["bkfrm"].getElementById("book").checked; 
0

有沒有這樣的ID在你的HTML代碼chkbx或書..使用下面的代碼

jQuery的

$(「輸入[名稱=」書']:勾選「)VAL()。