- 我希望能够选择图像(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();
});
})();