JQuery验证各个字段集 [英] JQuery Validate individual fieldsets

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

问题描述

我开发了一个包含多个字段集的表单,以表示填写完整表单的步骤。通过按钮单击(每个字段集一个)显示和隐藏字段集,但我想在显示下一个字段集之前验证每个字段集。

I've developed a form with multiple fieldsets to represent steps in filling out the complete form. The fieldsets are shown and hidden by button click (one on each fieldset) but I want to validate each fieldset before the next is shown.

我是JQuery的新手,我有点麻烦。我找到了这个指南( http:/ /encosia.com/2009/11/24/asp-net-webforms-validation-groups-with-jquery-validation/ )允许我独立验证不同的字段集但我的问题是如何使用该验证控制相关字段集的显示和隐藏。

I'm new to JQuery and I'm having a bit of trouble. I found this guide ( http://encosia.com/2009/11/24/asp-net-webforms-validation-groups-with-jquery-validation/) that allows me to validate different fieldsets independently but my problem is how do I use that validation to control the showing and hiding of the relevent fieldsets.

我认为这样做的方法是从按钮的每个点击事件创建一个函数,但我可以'似乎正确调用了验证功能。

I thought the way to do this would be to create a function from each click event for the buttons but I can't seem to call the validate function correctly.

恐怕我现在完全糊涂了!帮助!!

I'm afraid I'm completely confused now! Help!!

推荐答案

您可以为每个字段集编写自定义验证函数,并使用.keyup函数调用它。以下是一个示例:

You can write a custom validation function for each fieldset and call it using the .keyup function. Here is an example:

HTML:

<div id="fieldset1">
    <input type="text" name="fname" id="fname">
</div>

<div id="fieldset2">
    <!--hidden using css-->
    <input type="text" name="fname" id="fname">
</div>

Javascript:

Javascript:

$('#fieldset1 #fname').keyup(function () {
    // Runs every time your keystroke is released on this input
    if($(this).val() == 'Adam') {
        // Checks to see if the field's value is Adam. If it is, then it shows the next fieldset. Otherwise it hides it.
        $('#fieldset2').show();
    } else {
        $('#fieldset2').hide();
    }
}

这显然是一个非常简单的例子,但它说明了为了验证表单和根据用户修改DOM需要做什么输入。

This is obviously meant as a very simple example, but it illustrates what you will need to do in order to validate your form and modify the DOM based on user input.

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

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