Skip to content

Commit cb29def

Browse files
committed
Do not bind the confirm check twice on button elements inside a form.
Closes #384.
1 parent 358b19d commit cb29def

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/rails.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
linkClickSelector: 'a[data-confirm], a[data-method], a[data-remote], a[data-disable-with], a[data-disable]',
2626

2727
// Button elements bound by jquery-ujs
28-
buttonClickSelector: 'button[data-remote], button[data-confirm]',
28+
buttonClickSelector: 'button[data-remote]:not(form button), button[data-confirm]:not(form button)',
2929

3030
// Select elements bound by jquery-ujs
3131
inputChangeSelector: 'select[data-remote], input[data-remote], textarea[data-remote]',
@@ -356,6 +356,7 @@
356356

357357
$document.delegate(rails.buttonClickSelector, 'click.rails', function(e) {
358358
var button = $(this);
359+
359360
if (!rails.allowAction(button)) return rails.stopEverything(e);
360361

361362
if (button.is(rails.buttonDisableSelector)) rails.disableFormElement(button);

test/public/test/data-confirm.js

+19
Original file line numberDiff line numberDiff line change
@@ -223,3 +223,22 @@ asyncTest('binding to confirm:complete event of a button and returning false', 2
223223
start();
224224
}, 50);
225225
});
226+
227+
asyncTest('a button inside a form only confirms once', 1, function() {
228+
var confirmations = 0;
229+
window.confirm = function(msg) {
230+
confirmations++;
231+
return true;
232+
};
233+
234+
$('#qunit-fixture').append($('<form />').append($('<button />', {
235+
'data-remote': 'true',
236+
'data-confirm': 'Are you absolutely sure?',
237+
text: 'Click me'
238+
})));
239+
240+
$('form > button[data-confirm]').trigger('click');
241+
242+
ok(confirmations === 1, 'confirmation counter should be 1, but it was ' + confirmations);
243+
start();
244+
});

0 commit comments

Comments
 (0)