遍历所有表单元素,无论其类型如何 [英] Iterate through all form elements irrespective of their type

查看:100
本文介绍了遍历所有表单元素,无论其类型如何的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正忙于为一位客户提供表格管理器.通常的想法是,将为各个部门构建表单,我想创建一个微型系统,该系统将处理表单的验证等,而无需重复过多的工作.尽可能自动化.

I am busy with a form manager for one of our clients. The general idea is that forms will be built for the individual departments and I want to create a micro system which will handle validation etc for the form without redoing too much work. As automated as possible.

我的首要任务是单击提交按钮,遍历整个表单,并根据特定的凭据和验证规则在将表单发送到服务器之前对其进行验证.我已经看到了许多与获取表单中所有输入字段示例相关的帖子,但是我正在寻找一种解决方案,该解决方案将遍历整个表单及其内容,然后对所有表单元素(包括选择框,输入框,文本框等).

My first major task is that I want to on clicking the submit button, iterate through the entire form and based on specific credentials and validation rules validate the form before it gets sent to the server. I have seen a number of posts related to getting example all the input fields in a form, but I am looking for a solution which will iterate through the entire form and it's contents and then do validation on specifically all of the form elements (which includes select boxes, input boxes, text boxes etc).

我可以单独执行此操作,但是我希望验证从上至下进行,因此,如果第一个字段是选择字段的示例,下一个字段是输入字段,则它应该从上到下遍历表单,因此我可以据此输出错误.

I could do this separately, but I would preferably like the validation to be done top - bottom so if the first field is example a select and the next is an input field, it should iterate through the form from top to bottom so that I can output errors according to that.

// Sample code:
$(document).ready(function() {
    $("#main_submit").click(function() {
        // Select all of the form fields here and iterate through them ...
    });
});

<form name='test_form' method='POST' action=''>
    <div>
        <label for='title'>Title</label>
        <select name='title'>
            <option value=''>Please select ...</option>
            <option value='MR'>MR.</option>
        </select>
    </div>
    <div>
        <label for='full_name'>Full Name:</label>
        <input name='full_name' type='text' />
    </div>
    .....
</form>

推荐答案

使用jQuery :input选择器:

Use the jQuery :input selector:

jQuery("form :input");

描述:选择所有输入,文本区域,选择和按钮元素.

Description: Selects all input, textarea, select and button elements.

http://api.jquery.com/input-selector/

这篇关于遍历所有表单元素,无论其类型如何的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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