|
106 | 106 | form.__captchaSubmitButton = button; |
107 | 107 | }, |
108 | 108 |
|
109 | | - /** |
110 | | - * Create a temporary hidden input for the submit button's name/value |
111 | | - * @param {HTMLFormElement} form |
112 | | - * @returns {HTMLInputElement|null} The created input (to be cleaned up later) |
113 | | - * @private |
114 | | - */ |
115 | | - _createButtonInput: function (form) { |
116 | | - if (!form || !form.__captchaSubmitButton) return null; |
117 | | - |
118 | | - var button = form.__captchaSubmitButton; |
119 | | - if (!button.name || button.disabled) return null; |
120 | | - |
121 | | - // Create a temporary hidden input with the button's name/value |
122 | | - var input = document.createElement('input'); |
123 | | - input.type = 'hidden'; |
124 | | - input.name = button.name; |
125 | | - input.value = button.value || ''; |
126 | | - input.setAttribute('data-captcha-button-input', 'true'); |
127 | | - form.appendChild(input); |
128 | | - |
129 | | - return input; |
130 | | - }, |
131 | | - |
132 | | - /** |
133 | | - * Remove temporary button input after submit |
134 | | - * @param {HTMLFormElement} form |
135 | | - * @private |
136 | | - */ |
137 | | - _cleanupButtonInput: function (form) { |
138 | | - if (!form) return; |
139 | | - var input = form.querySelector('input[data-captcha-button-input="true"]'); |
140 | | - if (input) { |
141 | | - try { input.remove(); } catch (_) { /* IE11 fallback */ form.removeChild(input); } |
142 | | - } |
143 | | - }, |
144 | | - |
145 | 109 | /** |
146 | 110 | * Resubmit form honoring jQuery Unobtrusive AJAX and preserving submit button context |
| 111 | + * Uses modern requestSubmit() when available, falls back to trigger('submit') with hidden input |
147 | 112 | * @param {HTMLFormElement} form |
148 | 113 | * @param {string} reentryGuardName - Guard property name (e.g., '__captchaResubmit') |
149 | 114 | */ |
150 | 115 | resubmitForm: function (form, reentryGuardName) { |
151 | 116 | if (!form) return; |
152 | 117 | const guard = reentryGuardName || '__captchaResubmit'; |
153 | | - const isUnobtrusive = this.isUnobtrusiveAjax(form); |
| 118 | + const button = form.__captchaSubmitButton; |
154 | 119 |
|
155 | | - // Create temporary hidden input for the submit button (if present) |
156 | | - var buttonInput = this._createButtonInput(form); |
| 120 | + form[guard] = true; |
| 121 | + try { |
| 122 | + // Modern: Use requestSubmit (clean, native, includes button automatically) |
| 123 | + if (typeof form.requestSubmit === 'function') { |
| 124 | + form.requestSubmit(button || null); |
| 125 | + } |
| 126 | + // Fallback: jQuery Unobtrusive + temporary hidden input for legacy browsers |
| 127 | + else if (this.isUnobtrusiveAjax(form) && window.jQuery) { |
| 128 | + // Create temporary input for button name/value |
| 129 | + var tempInput = null; |
| 130 | + if (button && button.name && !button.disabled) { |
| 131 | + tempInput = document.createElement('input'); |
| 132 | + tempInput.type = 'hidden'; |
| 133 | + tempInput.name = button.name; |
| 134 | + tempInput.value = button.value || ''; |
| 135 | + form.appendChild(tempInput); |
| 136 | + } |
157 | 137 |
|
158 | | - if (isUnobtrusive && window.jQuery) { |
159 | | - form[guard] = true; |
160 | | - try { |
161 | 138 | window.jQuery(form).trigger('submit'); |
| 139 | + |
| 140 | + // Cleanup after serialization (100ms delay to let unobtrusive serialize first) |
| 141 | + if (tempInput) { |
| 142 | + setTimeout(function () { |
| 143 | + try { tempInput.remove(); } catch (_) { form.removeChild(tempInput); } |
| 144 | + }, 100); |
| 145 | + } |
162 | 146 | } |
163 | | - finally { |
164 | | - form[guard] = false; |
165 | | - // Cleanup after a short delay (let unobtrusive serialize the form first) |
166 | | - setTimeout(function () { this._cleanupButtonInput(form); }.bind(this), 100); |
| 147 | + // Last resort: native submit (no button context, but works) |
| 148 | + else { |
| 149 | + form.submit(); |
167 | 150 | } |
168 | | - } else { |
169 | | - form.submit(); |
170 | | - // Cleanup immediately for non-AJAX submits (form will navigate away anyway) |
171 | | - this._cleanupButtonInput(form); |
| 151 | + } finally { |
| 152 | + form[guard] = false; |
172 | 153 | } |
173 | 154 | }, |
174 | 155 |
|
|
0 commit comments