我想創建一個checkall複選框,將檢查所有使用java腳本創建的複選框。Checkall複選框通過javascript生成
我首先從用戶那裏得到行和列的no,然後使用javascript生成一個表格,然後在這些單元格中添加複選框。
現在我想創建一個checkall複選框,它將通過javascript檢查所有生成的複選框。
這是我如何我產生我寫至今細胞和checkboxes..My代碼...
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<script type="text/javascript">
function createTable() {
// alert("invoked")
var a, b, tableElem, rowElem, colElem, check,inc,hall,theatre,row,col;
a = document.getElementById('rows').value;
b = document.getElementById('cols').value;
hall = document.getElementById('theatreName').value;
theatre = document.getElementById('hallID').value;
document.getElementById('hdnhallName').value = hall;
document.getElementById('hdntheatreName').value = theatre;
document.getElementById("norow").value = a;
document.getElementById("nocol").value = b;
//alert(document.getElementById('hdntheatreName').value);
//alert(document.getElementById('hdnhallName').value);
inc=1;
if (a == "" || b == "") {
alert("Please enter some numeric value");
} else {
tableElem = document.createElement('table');
tableElem.style.border="1px solid black";
for (var i = 0; i < a; i++) {
rowElem = document.createElement('tr');
for (var j = 0; j < b; j++) {
colElem = document.createElement('td');
colElem.style.border="1px solid black";
colElem.appendChild(document.createTextNode(inc)); //to print cell number
rowElem.appendChild(colElem);
check = document.createElement('input');
check.type = "checkbox";
check.name = "chkSeat";
//check.value = "chk"+inc;
check.value = inc;
check.id = "chk"+inc;
//alert(check.value);
colElem.appendChild(check);
inc++;
//colElem.appendChild(lab);
}
tableElem.appendChild(rowElem);
}
document.getElementById("seat_tble2_td").appendChild(tableElem);
document.getElementById("sub_seat").disabled = false;
document.getElementById("chkall").disabled=false;
}
}
function checkall(bx){
var checkboxes = document.getElementsByName('chkSeat');
for (var i = 0; i < checkboxes.length; i++)
checkboxes[i].checked = source.checked;
}
</script>
<h1>Create seating plan</h1>
<table id="table_seat" border="1" width="500" >
<tr>
<td>
<!-- <form action="" name="f1" onsubmit=""> -->
<table width="100%" border="0" cellpadding="5" cellspacing="0">
<tr>
<td width="40%" height="40" class="txt">Theatre Name: </td>
<td width="60%"><input type="text" name="theatreName" id="theatreName"/></td>
</tr>
<tr>
<td height="46" class="txt">Hall ID : </td>
<td><input type="text" name="hallID" id="hallID"/></td>
</tr>
<tr>
<td height="46" class="txt">Rows : </td>
<td><input type="text" name="rows" id="rows"/></td>
</tr>
<tr>
<td height="46" class="txt">Columns : </td>
<td><input type="text" name="cols" id="cols"/></td>
</tr>
<tr>
<td align="right"> <input type="submit" name="Submit" value="Generate seat plan" onclick="createTable();"/></td>
<!--<td align="right"> <button style="width:20px; height:10px;" onclick="createTable();"></button></td>-->
<td valign="top" align="left"><input type="reset" name="Submit2" value=" Reset " /></td>
</tr>
</table>
<!-- </form> -->
</td>
</tr>
</table>
<form action="SeatMap" method="post" name="f1" onsubmit="">
<table id="seat_table2" cellpadding="10">
<tr>
<td id="seat_tble2_td">
</td>
</tr>
<tr>
<td id="seat_tble2_td1">
<input type="hidden" name="hdntheatreName" id="hdntheatreName"/>
<input type="hidden" name="hdnhallName" id="hdnhallName"/>
<input type="hidden" name="norow" id="norow"/>
<input type="hidden" name="nocol" id="nocol"/>
<input type="submit" value="Submit" id="sub_seat" name="sub_seat" disabled>
</td>
</tr>
<tr>
<td>
<input id="chkall" type="checkbox" name="chkall" onClick="checkAll(this);" disabled />Check All
</td>
</tr>
</table>
</form>
</body>
我試圖創建checkall但它不工作的JavaScript函數。請幫幫我。我使用JSP和Servlets進行服務器端編碼。在客戶端JavaScript甚至JQuery是好的,但我不知道JQuery。
但拉夫我怎麼能說出身體複選框..我創建的複選框JavaScript的。你是否試圖說我應該在javascrip本身中選擇他們的名字? –
它僅在第一次Gaurav時才完美無誤。當我取消選中並再次檢查時,它無效。 :( –
現在它工作正常...非常感謝Gaurav ... –