jQuery动态验证 [英] jQuery dynamic validation

查看:92
本文介绍了jQuery动态验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试使用jQuery验证插件做一些事情,而且似乎停留在一个地方.

Hi there Im trying to do a few things with jQuery validation plugin and seems stuck on one place.

这是一些代码:

<script type="text/javascript">
    $(document).ready(function(){
        $('#myform').validate();
        $('#email').rules('add', {required: true, messages: {required: 'email is required'}} );
        $('#phone').rules('add', {required: true, messages: {required: 'phone is required'}} );
        $('#validate').click(function(){
            $('#result').text($('#myform').validate().form());
            return false;
        });
    });
</script>
<form id="myform">
<div>email:<input type="text" id="email" /></div>
<div>phone:<input type="text" id="phone" /></div>
<div id="result"></div>

<input id="valdate" type="image" src="images/submit.png" />
</form>

结果是我不断收到错误的错误消息.如果我单击提交"按钮,则在电子邮件输入旁边会显示需要电话",而根本不会显示需要电子邮件".

As a result i keep getting a wrong error message. If i click on submit button 'phone is required' is shown near email input and 'email is required' isnt shown at all.

此代码有什么问题?

推荐答案

好的,看来我已经找到问题了.

OK It seems like I've found the problem.

必须 NAME 属性添加到元素中. 在我的示例中,如果将适当的名称添加到输入元素,它将很好用.

You HAVE to add NAME attribute to elements anyway. In my example if you add appropriate names to input elements it will work nice.

这篇关于jQuery动态验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆