BUGFIX Fixed change tracking of checkboxes and radiobuttons in jquery.changetracker plugin

This commit is contained in:
Ingo Schommer 2011-12-15 16:06:39 +01:00
parent ddad7fb110
commit 64f6b460e9
2 changed files with 18 additions and 14 deletions

View File

@ -54,19 +54,24 @@
this.initialize = function() {
// optional metadata plugin support
if ($.meta) options = $.extend({}, options, this.data());
// setup original values
this.getFields()
.bind('change', function(e) {
var onchange = function(e) {
var $field = $(e.target);
var origVal = $field.data('changetracker.origVal');
if(origVal === null || $field.val() != origVal) {
if(origVal === null || e.target.value != origVal) {
// TODO Also add class to radiobutton/checkbox siblings
$field.addClass(options.changedCssClass);
self.addClass(options.changedCssClass);
}
})
.each(function() {
$(this).data('changetracker.origVal', $(this).val());
};
// setup original values
var fields = this.getFields();
fields.filter(':radio,:checkbox').bind('click', onchange);
fields.not(':radio,:checkbox').bind('change', onchange);
fields.each(function() {
var origVal = $(this).is(':radio,:checkbox') ? self.find(':input[name=' + $(this).attr('name') + ']:checked').val() : $(this).val();
$(this).data('changetracker.origVal', origVal);
});
this.data('changetracker', true);

View File

@ -33,15 +33,14 @@ describe 'ChangeTracker'
it 'can track changes on input type=radio fields with existing values'
$('#form_test').append(
'<input type="radio" name="field_radio" value="1" />'
+ '<input type="radio" name="field_radio" value="2" />'
'<input type="radio" id="field_radio1" name="field_radio" value="1" checked="checked" />'
+ '<input type="radio" id="field_radio2" name="field_radio" value="2" />'
);
$('#form_test').changetracker();
$(':input[name=field_radio]').val(1).trigger('change');
$('#field_radio2').attr('checked', 'checked').trigger('click');
$('#form_test').is('.changed').should.be_true
$(':input[name=field_radio]').is('.changed').should.be_true
// $('#field_radio1').is('.changed').should.be_true
$('#field_radio2').is('.changed').should.be_true
end
it 'can track changes on select fields with existing values'