Skip to content

Commit e77fbe5

Browse files
committed
Checkboxradio: Properly find radio groups from the associated form
Fixes #9973 Closes jquerygh-1631
1 parent 5157c25 commit e77fbe5

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

tests/unit/checkboxradio/checkboxradio.html

+7
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@
6464
<label>
6565
<input type="checkbox" id="label-with-no-for"/>
6666
</label>
67+
68+
<form id="form3"></form>
69+
<input type="radio" name="crazy-form" id="crazy-form-1" form="form3" checked="checked">
70+
<label for="crazy-form-1">Choice 1</label>
71+
<input type="radio" name="crazy-form" id="crazy-form-2" form="form3">
72+
<label for="crazy-form-2">Choice 2</label>
73+
6774
</div>
6875
</body>
6976
</html>

tests/unit/checkboxradio/core.js

+19
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,25 @@ asyncTest( "Ensure checked after single click on checkbox label button", functio
4949
} );
5050
} );
5151

52+
test( "Handle form association via form attribute", function( assert ) {
53+
expect( 4 );
54+
55+
var radio1 = $( "#crazy-form-1" ).checkboxradio();
56+
var radio1Label = radio1.checkboxradio( "widget" );
57+
var radio2 = $( "#crazy-form-2" ).checkboxradio();
58+
var radio2Label = radio2.checkboxradio( "widget" );
59+
60+
radio2.change( function() {
61+
ok( this.checked, "#2 checked" );
62+
ok( !radio1[ 0 ].checked, "#1 not checked" );
63+
64+
assert.hasClasses( radio2Label, "ui-state-active" );
65+
assert.lacksClasses( radio1Label, "ui-state-active" );
66+
} );
67+
68+
radio2Label.simulate( "click" );
69+
} );
70+
5271
test( "Checkbox creation requires a label, and finds it in all cases", function( assert ) {
5372
expect( 7 );
5473
var groups = [

ui/widgets/checkboxradio.js

+13-8
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,6 @@ $.widget( "ui.checkboxradio", [ $.ui.formResetMixin, {
9393

9494
this._bindFormResetHandler();
9595

96-
// this.form is set by the form-reset-mixin
97-
this.formParent = this.form.length ? this.form : $( "body" );
98-
9996
if ( this.options.disabled == null ) {
10097
this.options.disabled = this.element[ 0 ].disabled;
10198
}
@@ -151,17 +148,25 @@ $.widget( "ui.checkboxradio", [ $.ui.formResetMixin, {
151148
},
152149

153150
_getRadioGroup: function() {
151+
var group;
154152
var name = this.element[ 0 ].name;
155-
var formParent = this.formParent[ 0 ];
153+
var nameSelector = "input[name='" + $.ui.escapeSelector( name ) + "']";
156154

157155
if ( !name ) {
158156
return $( [] );
159157
}
160158

161-
return this.formParent.find( "[name='" + $.ui.escapeSelector( name ) + "']" ).filter( function() {
162-
var form = $( this ).form();
163-
return ( form.length ? form : $( "body" ) )[ 0 ] === formParent;
164-
} ).not( this.element );
159+
if ( this.form.length ) {
160+
group = $( this.form[ 0 ].elements ).filter( nameSelector );
161+
} else {
162+
163+
// Not inside a form, check all inputs that also are not inside a form
164+
group = $( nameSelector ).filter( function() {
165+
return $( this ).form().length === 0;
166+
} );
167+
}
168+
169+
return group.not( this.element );
165170
},
166171

167172
_toggleClasses: function() {

0 commit comments

Comments
 (0)