javascript checkbox foreach problem in codeigniter
hi Im trying to incorporate a checkbox into my jquery table.. table data will be populated from a database.. under the table is buttons delete, edit, and add buttons.. I have a script that will check if a checkbox or tick or not.. so that when someone tries to delete a row and he isnt checked any checkbox , an alert will tell him that he/she should choose any row to delete first. the problem about my script is that if the table has only one row..even I tick the checkbox, it will still alert that I should choose a row to delete. Now here is my code:
javascript
<script type="text/javascript">
function validateForm()
{
var checkboxes = document.forms["list"]["checkID"];
for (var i = 0; i < checkboxes.length; i++) {
if (checkboxes[i].checked) {
return true;
}
}
alert("Please select a user to edit / delete.");
return false;
}
</script>
html
<table class="data display datatable" id="example">
<thead>
<tr>
<th>Username</th>
<th>Name</th>
<th>Nickname</th>
<th>Birthday</th>
<th>Sex</th>
<th>Address</th>
<th><input type="checkbox" id="select all"></th>
</tr>
</thead>
<tbody>
<?php fo开发者_如何学Pythonreach($query->result_array() as $row): ?>
<tr class="even gradeC">
<td><?php echo anchor('users/details/'.$row['usrID'],$row['usrName']);?></td>
<td><?php echo $row['usrpFirstName'].' '.$row['usrpLastName'];?></td>
<td><?php echo $row['usrpNick'];?></td>
<td><?php echo $row['usrpBday'];?></td>
<td><?php echo $row['usrpSex'];?></td>
<td><?php echo $row['usrpAddress'];?></td>
<td><input type="checkbox" name="checkID[]" id="checkID" value="<?php echo $row['usrID'];?>" />
<label for="checkID"></label></td>
</tr>
<? endforeach; ?>
</tbody>
</table>
<input type="submit" name="Delete" id="Delete" value="Delete" class="btn btn-orange" onclick='return validateForm()' />
<input type="submit" name="Edit" id="Edit" value="Edit" class="btn btn-orange" onclick='return validateForm()' />
<input type="button" name="AddUser" id="Add User" value="Add User" class="btn btn-orange" onclick="location.href='<? echo base_url().'users/add';?>'"/>
</form>
I tried out your code locally and after a while I got a soloution:
Change your javascript as follow:
function validateForm()
{
var checkboxes = document.forms["list"]["checkID"];
if (checkboxes.length == undefined)
{
if (checkboxes.checked) {
return true;
}
} else {
for (var i = 0; i < checkboxes.length; i++) {
if (checkboxes[i].checked) {
return true;
}
}
}
alert("Please select a user to edit / delete.");
return false;
}
I figured out, your first line in validateForm() is the source of all your trouble.
the problem about my script is that if the table has only one row
In this way the browser stores the elements in two different ways. If the form contains two or more elements with same name, he will return an array. If the form contains only one element, he will return an html object. In ord
check for checkboxes.length value.
If u check 4 checkbox. In ur script it will only check for 1 check box and returns back
check this code.It will work for u
<script type="text/javascript">
function validateForm()
{
var check = 0;
var checkboxes = document.forms["list"]["checkID"];
for (var i = 0; i <= checkboxes.length; i++) {
if (checkboxes[i].checked == false) {
check++;
}
}
if(check == 0)
return true;
else
{
alert("Please select a user to edit / delete.");
return false;
}
}
</script>
精彩评论