- 画像(li)を選択できるようにしたいのですが、選択項目の数は(メインdiv)のdata-maxとdata-minに依存します
問題は選択した機能にありますリスト内のアイテムのチェック済みステータスと選択済みクラスを削除しています
そのため、一番上のものを選択するまで機能し、混乱します。検証を変更して、trueまたはfalseを返すだけにする必要があります
デモ http://jsfiddle.net/XeELs/346/
JS
(function () {
var mobileForms = (function () {
return {
model: {
selection: {
validate: {
test: function (e) {
var that = this,
modelSelector = $('.divModelOptions'),
chosen = $('input:checked', modelSelector),
messages = $('.message', modelSelector),
minMessage = $('.min', modelSelector),
maxMessage = $('.max', modelSelector);
if (typeof (that.messageDisplayTime) !== 'undefined') clearTimeout(that.messageDisplayTime);
if (chosen.length >= that.min && chosen.length <= that.max) {
messages.slideUp(200);
return true;
} else if (chosen.length > that.max) {
if (that.max > 1) {
maxMessage.slideDown(400, function () {
that.messageDisplayTime = setTimeout(function () {
maxMessage.slideUp(200);
}, 3000);
});
if (typeof (e) !== 'undefined') e.preventDefault();
return false;
} else {
chosen[0].checked = false;
chosen.parents('li').removeClass('selected');
return true;
}
} else {
minMessage.slideDown(400);
return 'fail';
}
},
init: function () {
var modelSelector = $('.divModelOptions'),
html = '';
this.min = parseInt(modelSelector.attr('data-min'), 10);
this.max = parseInt(modelSelector.attr('data-max'), 10);
html = '\
<ul class="validation">\
<li class="message min">' + modelSelector.attr('data-min-msg') + '</li>\
<li class="message max">' + modelSelector.attr('data-max-msg') + '</li>\
</ul>';
modelSelector.prepend(html);
}
},
bindHandlers: function () {
var that = this,
modelSelector = $('.divModelOptions'),
chosen = $('input:checked', modelSelector);
var max = parseInt(modelSelector.attr('data-max'), 2);
$('input', modelSelector).click(function (e) {
var parent = $(this).parents('li');
if (that.validate.test(e)) {
if (parent.hasClass('selected')) {
parent.removeClass('selected');
} else {
parent.addClass('selected');
}
}
});
if (chosen.length) {
chosen.parents('li').addClass('selected');
that.validate.test();
}
$('form').submit(function (e) {
if (!that.validate.test() || that.validate.test() === 'fail') e.preventDefault();
});
},
init: function () {
this.validate.init();
this.bindHandlers();
}
},
nextButton: {
bindHandlers: function (button) {
button.one('click', function () {
mobileForms.showNextStep();
});
},
create: function () {
var nextCommandText = $('.divModelOptions').data('nextcommand');
// TODO: Next button should start off grey and become blue once a model is selected.
var button = $('<input type="button" value="' + nextCommandText + '" class="next-button">');
$('.divModelOptions').append(button);
this.bindHandlers(button);
},
init: function () {
this.create();
}
},
init: function () {
this.selection.init();
this.nextButton.init();
}
},
validation: {
focusOnError: function (messages) {
var message = $(messages[0]);
$('html, body').animate({
scrollTop: message.offset().top - 100
}, 500);
},
init: function () {
var visibleMessages;
$('form').submit(function () {
visibleMessages = $('.validation-message:visible, .validation .message:visible');
if (visibleMessages.length) {
mobileForms.validation.focusOnError(visibleMessages);
}
});
}
},
init: function () {
this.model.init();
this.validation.init();
}
};
})();
$(function () {
mobileForms.init();
});
})();