2010-02-22 45 views
2

我的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> 

我想在他们出现在浏览器上$(document).ready()序列中的形式输入的所有元素的列表。我写了下面的语句jQuery中列出了在序列中的形式input元素出现在浏览器:

var textboxes = $("#form1 :input") 
  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** 
}); 

问题:

  1. 我怎么能得到他们出现在浏览器上的在序列表格中的所有元素的列表$(document).ready()包括所有输入文本框,单选按钮组,复选框,选择框,文本区和按钮?
  2. 以上声明将上述单选按钮视为单独的单选按钮,并不认为它们是一个组,但实际上在单选按钮之上是同一组。 那么如何解决这些问题呢?

请帮助我的朋友! 谢谢!

回答

6
$('#form1 input,#form1 select,#form1 textarea') 

$('#form1').find('select,input,textarea') 

更新了答案2:

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

您可以找到input[type=radio]:checked选择的无线电输入,但如果组中没有无线电选项被选中,你不会得到任何选择。 JQuery的应该以相同的顺序解析此它们在DOM中发现

+0

谢谢你jjacka!期待你的答复。 – 2010-02-22 14:59:17

+0

谢谢你jjacka!你的解决方案正在解决我的第一个问题。 非常感谢! – 2010-02-22 15:07:22

1

如果你想在它们出现的实际顺序,试试这个:

$("#form1").find("*").filter(function() { 
    return !jQuery.inArray(this.tagName.toUpperCase(), ["INPUT","SELECT","TEXTAREA"]); 
}) 
+0

@Gumbo - jjacka的解决方案不是确保正确订单的可靠方法吗?换句话说,使用该解决方案,jQuery如何表现?它是否按请求的顺序为每个选择器单独查找,然后按照该顺序添加相应的结果? – user113716 2010-02-22 15:07:47

+0

@patrick:我认为jQuery将分别选择每种类型,然后合并结果。这意味着首先是所有的'select'元素,然后是'input'元素,然后是'textarea'元素。但我可能是错的。 – Gumbo 2010-02-22 15:20:51

+0

+1花时间回复。谢谢。 – user113716 2010-02-22 15:30:58

1
var data = $('#form1').serialize(); 

然后做一些正则表达式来拿到钥匙出字符串:-)

相关问题