简体   繁体   English

使用Jquery遍历表单中的元素

[英]Loop through elements in a form using Jquery

How can i loop through every elements in a form. 我如何遍历表单中的每个元素。 i want to store the name if any update happens for the element. 我想存储名称,如果该元素发生任何更新。 My code working correctly for input types. 我的代码对于输入类型正常工作。 But i didn't get desired output incase of select field. 但是在选择字段的情况下我没有得到想要的输出。

<html>
<form method="post" action="" id="basic_profile" class="basic_profile">
    <fieldset class="lang_content" dir="ltr" id="fieldset-en">
        <legend>English</legend>
        <dl>
            <dd>
                <select id="cardtype" name="cardtype">
                    <option value="debit-0.00-50.00">Debit1</option>
                    <option selected='selected' value="debit-0.00-50.00">Debit2</option>
                    <option value="debit-0.00-50.00">Debit3</option>
                    <option value="Credit-1.00-50.00">CreditCard</option>
                </select>
            </dd> 
            <dt id="business_name_en-label"><label class="optional" for="business_name_en">Profile Name / Business Entity Name</label></dt>

            <dd id="business_name_en-element">
                <input type="text" value="New Tesr" id="business_name_en" name="business_name_en">
            </dd> <dt id="description_en-label"><label class="optional" for="description_en">Description</label></dt>

            <dd id="description_en-element">
                <textarea cols="50" rows="4" id="description_en" name="description_en">new test description</textarea>
            </dd> <dt id="contact_person_en-label"><label class="optional" for="contact_person_en">Contact Name</label></dt>

            <dd id="contact_person_en-element">
                <input type="text" value="New test" id="contact_person_en" name="contact_person_en">
            </dd> <dt id="street_en-label"><label class="optional" for="street_en">Street</label></dt>

            <dd id="street_en-element">
                <input type="text" value="" id="street_en" name="street_en">
            </dd> <dt id="area_en-label"><label class="optional" for="area_en">Area</label></dt>

            <dd id="area_en-element">
                <input type="text" value="" id="area_en" name="area_en">
            </dd>
        </dl>
    </fieldset>
    <input type="submit" value="Save" id="save" name="save">
</form>

</html>

And my jquery is: 我的jQuery是:

<script type="text/javascript">
$("#save").click(function() {
    var info = '';
    $("#basic_profile dl dd ").each(function() {
        if (this.defaultValue !== this.value)
            if (info === '')
                info += this.name;
            else
                info += ',' + this.name;
    });

    alert(info);
    $('#form_info').val(info);
    return false;

 });
</script>

you can try: 你可以试试:

$('#basic_profile').find(':input').each(function(){

});

This will select all input ,selects, textarea etc. 这将选择所有输入,选择,文本区域等。

Try using defaultValue attribute on input/select tag. 尝试在输入/选择标签上使用defaultValue属性。

$('#basic_profile').find(':input').each(function(){

    var defaultValue = $(this).prop('defaultValue);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM