【jQuery示例】遍歷表單數據並顯示

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4   <style>
 5   body, select { font-size:14px; }
 6   form { margin:5px; }
 7   p { color:red; margin:5px; }
 8   b { color:blue; }
 9   </style>
10   <script src="http://code.jquery.com/jquery-latest.js"></script>
11 </head>
12 <body>
13   <p><b>Results:</b> <span id="results"></span></p>
14  
15   <form>
16     <select name="single">
17       <option>Single</option>
18       <option>Single2</option>
19  
20     </select>
21     <select name="multiple" multiple="multiple">
22       <option selected="selected">Multiple</option>
23       <option>Multiple2</option>
24  
25       <option selected="selected">Multiple3</option>
26     </select><br/>
27     <input type="checkbox" name="check" value="check1" id="ch1"/>
28  
29     <label for="ch1">check1</label>
30     <input type="checkbox" name="check" value="check2" checked="checked" id="ch2"/>
31  
32     <label for="ch2">check2</label>
33     <input type="radio" name="radio" value="radio1" checked="checked" id="r1"/>
34  
35     <label for="r1">radio1</label>
36     <input type="radio" name="radio" value="radio2" id="r2"/>
37  
38     <label for="r2">radio2</label>
39   </form>
40 <script>
41   function showValues() {
42     var fields = $(":input").serializeArray();
43     $("#results").empty();
44     jQuery.each(fields, function(i, field){
45       $("#results").append(field.value + " ");
46     });
47   }
48  
49   $(":checkbox, :radio").click(showValues);
50   $("select").change(showValues);
51   showValues();
52 </script>
53  
54 </body>
55 </html>

從表單獲取值,遍歷並將其顯示出來html

:input:選擇全部 input, textarea, select 和 button 元素.jquery

$(':checkbox') 等同於 $('[type=checkbox]')。如同其餘僞類選擇器(那些以「:」開始)建議前面加上一個標記名稱或其餘選擇器;不然,默認使用通用選擇("*")。換句話說$(':checkbox') 等同於 $('*:checkbox'),因此應該使用$('input:checkbox')來提高效率。數組

$(':radio')等價於$('[type=radio]')。如同其餘僞類選擇器(那些以「:」開始),建議使用此類選擇器時,跟在一個標籤名或者其它選擇器後面,默認使用了全局通配符選擇器 "*"。換句話說$(':radio') 等同於 $('*:radio'),因此應該使用$('input:radio')app

jQuery.each( collection, callback(indexInArray, valueOfElement) )

一個通用的迭代函數,它能夠用來無縫迭代對象和數組。數組和相似數組的對象經過一個長度屬性(如一個函數的參數對象)來迭代數字索引,從0到length - 1。其餘對象經過其屬性名進行迭代。ide

$.each([52, 97], function(index, value) {
  alert(index + ': ' + value);
});
//1:52
//1:97
var obj = {
  "flammable": "inflammable",
  "duh": "no duh"
};
$.each( obj, function( key, value ) {
  alert( key + ": " + value );
});
//flammable: inflammable 
//duh: no duh
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>jQuery.each demo</title>
  <style>
  div {
    color: blue;
  }
  div#five {
    color: red;
  }
  </style>
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
</head>
<body>
 
<div ></div>
<div ></div>
<div ></div>
<div ></div>
<div ></div>
 
<script>
var arr = [ "one", "two", "three", "four", "five" ];
var obj = { one: 1, two: 2, three: 3, four: 4, five: 5 };
 
jQuery.each( arr, function( i, val ) {
  $( "#" + val ).text( "Mine is " + val + "." );
 
  // Will stop running after "three"
  return ( val !== "three" );
});
 
jQuery.each( obj, function( i, val ) {
  $( "#" + i ).append( document.createTextNode( " - " + val ) );
});
</script>
 
</body>
</html>
Mine is one. - 1
Mine is two. - 2
Mine is three. - 3
- 4
- 5
相關文章
相關標籤/搜索