jquery选取表单元素的方法

来源:互联网 发布:mysql安装教程mac 编辑:程序博客网 时间:2024/06/05 01:06

      在jsp的表单验证中,有时候我们需要检查一下输入的账号和密码是否为空,可以使用jquery进行检查。但是,假如表单是像以下这样写的:

     

<form>    账号:<input type="text" name="username" /><br/>    密码:<input  type="password" name="password" /><br/>    性别:<input type="radio" name="sex" value=""><input type="radio" name="sex" value="">    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;    <input  type="button"  value="登陆"/></form>

     那么如何判断账号和密码是否为空或者不满足要求了?

     答案如下:


   

<script type="text/javascript">    $(function(){        //属性过滤选择器,type,id,name,classtitle都属于属性        $('form input[type=button]').click(function(){            if($('form input[type=text]').val()==""||$('form input[type=text]').length<6){                alert('请输入正确的账号');            }        });    });</script>

   使用元素属性选择器,在表单中,type,class,id,name,title都可以看做属性,那么这种方法就是一种通用的方法,可以省去添加诸如id,class等属性



原创粉丝点击