javascript 在codeigniter中使用javascript进行表单验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6133977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
form validation using javascript in codeigniter
提问by kester martinez
hi i have a jquery table populate from datebase:
嗨,我有一个从数据库填充的 jquery 表:
<?php
$attributes = array('name' => 'users');
echo form_open('users/action',$attributes);?>
<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 foreach($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" />
<input type="submit" name="Edit" id="Edit" value="Edit" class="btn btn-orange" onclick="return validateForm()" />
<input type="submit" name="AddUser" id="Add User" value="Add User" class="btn btn-orange" />
</form>
as you can see below,, i use checkID[]
so that i can get every id of each user.
i did make a onlick
on edit button so that it will check if the id is empty or not. heres my js function
正如你在下面看到的,我使用checkID[]
这样我可以获得每个用户的每个 ID。我确实做了一个onlick
编辑按钮,以便它检查 id 是否为空。这是我的 js 函数
<head>
<script type="text/javascript">
function validateForm()
{
var x=document.forms["users"]["checkID[]"].value
if (x==null || x=="")
{
alert("Please select user to edit.");
return false;
}
}
</script>
</head>
the problem now is that it will still alert even i check one user in the table..
现在的问题是即使我检查表中的一个用户它仍然会发出警报..
even i use this:
即使我使用这个:
<?php
$attributes = array('name' => 'users' , 'onsubmit' =>"return validateForm()");
echo form_open('users/action',$attributes);?>
回答by takteek
document.forms["users"]["checkID[]"]
will give you an array of allthe checkboxes with that name. You're asking for the value
of the array which doesn't make sense. You'll need to loop through them to find out if any are checked with something like this:
document.forms["users"]["checkID[]"]
将为您提供具有该名称的所有复选框的数组。您要求的value
是没有意义的数组。您需要遍历它们以查明是否有这样的检查:
function validateForm() {
var checkboxes = document.forms["users"]["checkID[]"];
for (var i = 0; i < checkboxes.length; i++) {
if (checkboxes[i].checked) {
return true;
}
}
alert("Please select a user to edit.");
return false;
}
I haven't tested it but that code should give you the general idea.
我还没有测试过,但该代码应该给你一个大致的想法。