Skip to content
This repository
Browse code

For the sake of jQuery’s serialize, submit inputs’ values are added t…

…o the form as a hidden input—this ensures it will only happen once, and that this logic only applies to submit inputs with a name attribute.
  • Loading branch information...
commit ecc1361f7f969e893d209387028acfd5bd6abc41 1 parent dec502d
Mat Marquis authored September 23, 2011

Showing 1 changed file with 20 additions and 16 deletions. Show diff stats Hide diff stats

  1. 36  js/jquery.mobile.forms.button.js
36  js/jquery.mobile.forms.button.js
@@ -21,7 +21,9 @@ $.widget( "mobile.button", $.mobile.widget, {
21 21
 	_create: function() {
22 22
 		var $el = this.element,
23 23
 			o = this.options,
24  
-			type;
  24
+			type,
  25
+			name,
  26
+			$buttonPlaceholder;
25 27
 
26 28
 		// Add ARIA role
27 29
 		this.button = $( "<div></div>" )
@@ -38,25 +40,27 @@ $.widget( "mobile.button", $.mobile.widget, {
38 40
 			.insertBefore( $el )
39 41
 			.append( $el.addClass( "ui-btn-hidden" ) );
40 42
 
41  
-		// Add hidden input during submit
42 43
 		type = $el.attr( "type" );
  44
+		name = $el.attr( "name" );
43 45
 
44  
-		if ( type !== "button" && type !== "reset" ) {
  46
+		// Add hidden input during submit if input type="submit" has a name.
  47
+		if ( type !== "button" && type !== "reset" && name ) {
  48
+				$el.bind( "vclick", function() {
  49
+					// Add hidden input if it doesn’t already exist.
  50
+					if( $buttonPlaceholder === undefined ) {
  51
+						$buttonPlaceholder = $( "<input>", {
  52
+									type: "hidden",
  53
+									name: $el.attr( "name" ),
  54
+									value: $el.attr( "value" )
  55
+								})
  56
+								.insertBefore( $el );
45 57
 
46  
-			$el.bind( "vclick", function() {
47  
-
48  
-				var $buttonPlaceholder = $( "<input>", {
49  
-							type: "hidden",
50  
-							name: $el.attr( "name" ),
51  
-							value: $el.attr( "value" )
52  
-						})
53  
-						.insertBefore( $el );
54  
-
55  
-				// Bind to doc to remove after submit handling
56  
-				$( document ).submit(function(){
57  
-					 $buttonPlaceholder.remove();
  58
+						// Bind to doc to remove after submit handling
  59
+						$( document ).submit(function(){
  60
+							 $buttonPlaceholder.remove();
  61
+						});
  62
+					}
58 63
 				});
59  
-			});
60 64
 		}
61 65
 
62 66
 		this.refresh();

0 notes on commit ecc1361

Please sign in to comment.
Something went wrong with that request. Please try again.