|
16 | 16 | * 2. Add 'gw-prevent-duplicates' to the CSS Class Name setting for any field in which duplicate selections |
17 | 17 | * should be prevented. |
18 | 18 | */ |
19 | | -$checkboxes = $( '.gw-prevent-duplicates' ).find( 'input' ); |
| 19 | +$inputs = $( '.gw-prevent-duplicates' ).find( 'input' ); |
20 | 20 |
|
21 | | -$checkboxes.click( function() { |
22 | | - gwDisableDuplicates( $( this ), $checkboxes ); |
| 21 | +$inputs.click( function() { |
| 22 | + gwDisableDuplicates( $( this ), $inputs ); |
23 | 23 | } ); |
24 | 24 |
|
25 | | -$checkboxes.each( function() { |
26 | | - gwDisableDuplicates( $( this ), $checkboxes ); |
| 25 | +$inputs.each( function() { |
| 26 | + gwDisableDuplicates( $( this ), $inputs ); |
27 | 27 | } ); |
28 | 28 |
|
29 | 29 | function gwDisableDuplicates( $elem, $group ) { |
30 | | - let value = $elem.val(); |
31 | | - $group |
32 | | - .not( $elem ) |
| 30 | + |
| 31 | + let value = $elem.val(); |
| 32 | + let $targets = $group.not( $elem ); |
| 33 | + let isChecked = $elem.is( ':checked' ); |
| 34 | + // We use this to instruct Gravity Forms not to re-enable disabled duplicate options when |
| 35 | + // that option is revealed by conditional logic. |
| 36 | + let disabledClass = 'gf-default-disabled'; |
| 37 | + let previousValue; |
| 38 | + |
| 39 | + // Only one choice can be selected in a Radio Button field while multiple choices |
| 40 | + // can be selected in a Checkbox field. This logic handles saving/retrieving the previous |
| 41 | + // value and re-enabling inputs with the previous value. |
| 42 | + if ( $elem.is( ':radio' ) ) { |
| 43 | + previousValue = $elem.parents( '.gfield' ).data( 'previous-value' ); |
| 44 | + $elem.parents( '.gfield' ).data( 'previous-value', $elem.val() ); |
| 45 | + if ( previousValue ) { |
| 46 | + $targets |
| 47 | + .filter( '[value="{0}"]'.format( previousValue ) ) |
| 48 | + .prop( 'disabled', false ) |
| 49 | + .removeClass( disabledClass ); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + let $filteredTargets = $targets |
33 | 54 | .filter( '[value="{0}"]'.format( value ) ) |
34 | 55 | .prop( 'disabled', $elem.is( ':checked' ) ); |
| 56 | + |
| 57 | + if ( isChecked ) { |
| 58 | + $filteredTargets.addClass( disabledClass ); |
| 59 | + } else { |
| 60 | + $filteredTargets.removeClass( disabledClass ); |
| 61 | + } |
| 62 | + |
35 | 63 | } |
0 commit comments