From d06cc9d16e33ce7d924a542db46f738630647ebe Mon Sep 17 00:00:00 2001 From: Miles Fink Date: Tue, 28 Apr 2015 13:13:29 -0400 Subject: [PATCH] Fix issues related to group repeatable subsequent rows when there is a checkbox or radio input. If the input is a checkbox or radio, the original value is needed or saving will not save the value (subsequent post saves work because inputs will then have the required value set). This patch conditionally adds values to radios and inputs on new repeatable and sets the checked attribute only on the default option (radios). Fixes issue #257 (this will set reset text inputs as well), and #246 & #263(restores radio and checkbox values on new row saves) --- js/cmb2.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/js/cmb2.js b/js/cmb2.js index f784c6a79..8b588f22c 100755 --- a/js/cmb2.js +++ b/js/cmb2.js @@ -344,6 +344,7 @@ window.CMB2 = (function(window, document, $, undefined){ var $newInput = $(this); var isEditor = $newInput.hasClass( 'wp-editor-area' ); var oldFor = $newInput.attr( 'for' ); + var oldVal = $newInput.val(); // var $next = $newInput.next(); var attrs = {}; var newID, oldID; @@ -358,14 +359,22 @@ window.CMB2 = (function(window, document, $, undefined){ attrs = { id: newID, name: newName, - // value: '', + value: '', 'data-iterator': cmb.idNumber, }; + if ( ($newInput.is(':checkbox') || $newInput.is(':radio') ) ) { + if (oldVal == "") { + attrs.checked = "checked"; + } else { + attrs.value = oldVal; + $newInput.removeAttr("checked"); + } + } } $newInput .removeClass( 'hasDatepicker' ) - .attr( attrs ).val(''); + .attr( attrs ); // wysiwyg field if ( isEditor ) {