0
我得到了2个alert();并试图使用'返回错误',并期望它会起作用。返回false在JavaScript对象中不工作
$(function() {
var options = ['mandatory', 'email'];
var validation_event = {
mandatory: function(that) {
if (!$(that).val() && $(that).data('placeholder')) {
alert('Please fill in ' + $(that).data('placeholder') + '.');
return false; // this is not working? I want to alert only one alert
}
}
}
function init() {
$('input,select,textarea').each(function() {
var self = $(this);
$.each(options, function(i, val) {
var attr = self.attr(val); // option array
if (typeof attr !== typeof undefined && attr !== false) {
validation_event[val](self);
}
});
});
}
$('#submit').click(function(e) {
e.preventDefault();
init();
})
})
正试图编写一个验证逻辑,我可以每次重复使用。
https://jsfiddle.net/m5jsscp7/
可我更简单的逻辑吗?只是不写太多的回报真实;可能? –
为什么这不起作用? http://pastebin.com/PiGazwCW –