Skip to content

Commit 4afcecd

Browse files
setValue(): added option to trigger change event
1 parent bcfb2f2 commit 4afcecd

File tree

1 file changed

+45
-12
lines changed

1 file changed

+45
-12
lines changed

assets/FormHelper/FormHelper.js

+45-12
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,14 @@ wsYii2.FormHelper = {
8181
* Set form input value(s) of any type of input
8282
*
8383
* @param {string|object} inputName - Name of form field or jQuery object of the element
84+
* @param {object} options - Available options:
85+
* - `triggerChange` : set true to trigger a change event on the input if the new value is different (see also https://stackoverflow.com/questions/3179385/val-doesnt-trigger-change-in-jquery and https://stackoverflow.com/questions/24410581/changing-prop-using-jquery-does-not-trigger-change-event)
8486
*
8587
* @return {object|null} - The element as a jQuery object
8688
*/
87-
setValue: function(inputName, value) {
89+
setValue: function(inputName, value, options) {
8890
var $inputElement;
91+
if (typeof options == 'undefined') options = {};
8992

9093
if (typeof inputName === 'string') {
9194
$inputElement = $(':input[name="' + inputName + '"]');
@@ -97,39 +100,69 @@ wsYii2.FormHelper = {
97100
switch ($inputElement.attr('type')) {
98101
case 'checkbox':
99102
$inputElement.each(function (i) {
103+
var $this = $(this);
100104
if (Array.isArray(value)) {
101105
if (value.length == 0) {
102-
$(this).attr('checked', false);
106+
if (options.triggerChange === true && $this.prop('checked') !== false) {
107+
$this.prop('checked', false).trigger('change');
108+
} else {
109+
$this.prop('checked', false);
110+
}
103111
} else {
104-
var currElement = this, setValue = false;
112+
var setValue = false;
105113
$.each(value, function(indx, currValue) {
106-
if ($(currElement).val() == currValue) {
114+
if ($this.val() == currValue) {
107115
setValue = true;
108116
return false;
109117
}
110118
});
111-
$(this).attr('checked', setValue);
119+
if (options.triggerChange === true && $this.prop('checked') !== setValue) {
120+
$this.prop('checked', setValue).trigger('change');
121+
} else {
122+
$this.prop('checked', setValue);
123+
}
112124
}
113125
} else {
114-
if ($(this).val() == value) {
115-
$(this).attr('checked', true);
126+
if ($this.val() == value) {
127+
if (options.triggerChange === true && $this.prop('checked') !== true) {
128+
$this.prop('checked', true).trigger('change');
129+
} else {
130+
$this.prop('checked', true);
131+
}
116132
} else {
117-
$(this).attr('checked', false);
133+
if (options.triggerChange === true && $this.prop('checked') !== false) {
134+
$this.prop('checked', false).trigger('change');
135+
} else {
136+
$this.prop('checked', false);
137+
}
118138
}
119139
}
120140
});
121141
break;
122142
case 'radio':
123143
$inputElement.each(function (i) {
124-
if ($(this).val() == value) {
125-
$(this).attr('checked', true);
144+
var $this = $(this);
145+
if ($this.val() == value) {
146+
if (options.triggerChange === true && $this.prop('checked') !== true) {
147+
$this.prop('checked', true).trigger('change');
148+
} else {
149+
$this.prop('checked', true);
150+
}
126151
} else {
127-
$(this).attr('checked', false);
152+
if (options.triggerChange === true && $this.prop('checked') !== false) {
153+
$this.prop('checked', false).trigger('change');
154+
} else {
155+
$this.prop('checked', false);
156+
}
128157
}
129158
});
130159
break;
131160
default:
132-
$inputElement.val(value);
161+
if (options.triggerChange === true && $inputElement.val() != value) {
162+
$inputElement.val(value).trigger('change');
163+
} else {
164+
$inputElement.val(value);
165+
}
133166
break;
134167
}
135168
return $inputElement;

0 commit comments

Comments
 (0)