使用jQuery在表单字段中进行迭代 [英] iterate in form fields using jquery

查看:58
本文介绍了使用jQuery在表单字段中进行迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有表格,并且它有很多表格字段.如何在所有表单字段中进行迭代并检查输入类型并收集值

suppose i have form and it has many form fields. how can i iterate with in all form fields and check the input type and collect the value

我的表单字段是

<form id="form1" runat="server">
<div>
    <fieldset>
        <ol>
            <li>
                <label class="left">
                    First Name
                </label>
                <input type="text" id="FirstName" runat="server" /></li>
            <li>
                <label class="left">
                    Last Name
                </label>
                <input type="text" id="LastName" runat="server" /></li>
            <li>
                <label class="left">
                    Email
                </label>
                <input type="text" id="Email" runat="server" /></li>
            <li>
                <label class="left">
                    Phone
                </label>
                <input type="text" id="Phone" runat="server" /></li>
            <li>
                <label class="left">
                    Contact Method
                </label>
                <span class="checkBoxGroup">
                    <input type="checkbox" id="ReqEmail" runat="server" /><label>Email</label>
                    <input type="checkbox" id="ReqMail" runat="server" /><label>Mail</label>
                    <input type="checkbox" id="ReqPhone" runat="server" /><label>Phone</label>
                    <input type="checkbox" id="ReqNoContact" runat="server" /><label>No Contact</label>
                </span></li>
            <li>
                <label class="left">
                    New Letter Type
                </label>
                <span class="myGroupRandom" >
                    <input type="checkbox" id="Checkbox1" runat="server" /><label>Company News</label>
                    <input type="checkbox" id="Checkbox2" runat="server" /><label>Press Releases</label>
                    <input type="checkbox" id="Checkbox3" runat="server" /><label>Deals</label>
                    <input type="checkbox" id="Checkbox4" runat="server" /><label>Employement</label>
                </span></li>
            <li>
                <input type="submit" id="Submit" value="Submit" /></li>
        </ol>
    </fieldset>
</div>
</form>

请指导谢谢

推荐答案

您可以使用 each :

You could use each:

$.each($("input"), function(index, item) {
    var type = $(this).attr("type");
    var val = $(this).val();
    //do your checking...
    if (type == "text") {
        //and so on...
    }
});

请注意,我使用$(this)而不是item参数访问项目-仅使用input元素时,使用item可能不会引起问题,但是我过去遇到过麻烦使用item访问元素的属性和方法时,通常请使用$(this).

Note that I use $(this) to access the item, rather than the item parameter argument - using item might not cause an issue when working with only input elements, but I have had trouble in the past when accessing properties and methods of an element using the item, so generally resort to $(this).

这篇关于使用jQuery在表单字段中进行迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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