如何在jquery中使用$()函数在表单中列出所有输入字段?

Par*_*nak 2 each jquery

我的HTML表单如下所示

<form id="form1" name="form1" method="post" action="">
number: <input name="formnum" type="text" id="input_1" tabindex="1" size="30" maxlength="30" />
Title:
<select name="title" id="input_2" tabindex="2" >
   <option selected="selected" value=""></option>
   <option  value="Mr." id="option_1 ">Mr.</option>
   <option  value="Mrs." id="option_2">Mrs.</option>
   <option  value="Ms." id="option_3">Ms.</option>
   <option  value="Dr." id="option_4">Dr.</option>
</select>
<label> <input type="radio" name="gender" value="Male" id="input_3" tabindex="3"/>Male </label>
<label> <input type="radio" name="gender" value="Female" id="input_3"/> Female</label>
First Name:
<input name="fname" type="text" id="input_5" tabindex="4" value="" size="30" maxlength="30"  />
Last Name:
<input name="lname" type="text" id="input_6" tabindex="5" value="" size="30" maxlength="30"  /> 
Address:
<input name="address" type="text" id="input_7" tabindex="6" value="" size="30" maxlength="30"  />
<input type="submit" name="Submit" value="Submit" tabindex="16" />
<input type="reset" name="Submit2" value="Reset" tabindex="17" />
</form>
Run Code Online (Sandbox Code Playgroud)

我想按照它们在浏览器中出现的顺序列出表单中所有输入元素的列表$(document).ready().我在jQuery中编写了以下语句,以input按照它们在浏览器中出现的顺序列出表单中的元素:

var textboxes = $("#form1 :input")
Run Code Online (Sandbox Code Playgroud)
  1. 但是这个语句只给出了带有input标签的元素列表,即所有文本框,单选按钮和提交按钮.
  2. 它不包括给定列表中的选择框,文本区域.

这是我的jQuery代码:

$(document).ready(function(){

var textboxes = $("#form1 :input"), //gets all the textboxes         
        images = $("img.classforimg");   //gets all the images
    images.hide(); //hide all of them except the first one
alert(textboxes.length);
    textboxes.each(function (i){
                var j = i;
                    $(this).focus(function (){
                images.hide(); //hide all of them except the first one
                                images.eq(0).show();
                images.eq(j).show();
                });
            });

**Here Jquery code for form validation**
**Here Jquery code of Custom validation methods**
});
Run Code Online (Sandbox Code Playgroud)

问题:

  1. 如何按照它们在浏览器中显示的顺序获取表单中所有元素的列表,$(document).ready()包括所有输入文本框,单选按钮组,复选框,选择框,textareas和按钮?
  2. 以上语句将上述形式的单选按钮视为单独的单选按钮,并不将它们视为一组,但实际上单选按钮属于同一组.那么如何解决这些问题呢?

请各位朋友帮帮我!谢谢!

jja*_*cka 6

 $('#form1 input,#form1 select,#form1 textarea')
Run Code Online (Sandbox Code Playgroud)

要么

$('#form1').find('select,input,textarea')
Run Code Online (Sandbox Code Playgroud)

更新回答2:

$('#form1').find('select,input[type!=radio],textarea,input[type=radio]:checked')
Run Code Online (Sandbox Code Playgroud)

您可以找到所选的无线电输入input[type=radio]:checked,但如果未选择组中的无线电选项,您将无法获得任何选项.JQuery应该按照它们在DOM中找到的顺序解析它