开发者

iterate in form fields using 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

my form fields are

<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>C开发者_C百科ompany 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>

please guide thanks


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...
    }
});

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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜