我是jquery新手,在出现错误的情况下在js文件中实现了以下功能-
@Bean(name = "pimAmqpAdmin")
public AmqpAdmin pimAmqpAdmin(@Qualifier("defaultConnectionFactory") ConnectionFactory connectionFactory) {
return new RabbitAdmin(connectionFactory);
}
我在这里重复以下几行,必须替换为标准的一个-
function before_submit_check_if_has_blank_field() {
$("[type=submit]").click(function(e) {
var of_key_field = $('.key_field')
var of_val_field = $('.val_field')
var should_run = 1
if (of_key_field.length === 1) {
$('.key_field:not(:first)').filter(function() {
if (this.value === "") {
alert('Please Fill In Or Remove The Blank Fields First')
e.preventDefault();
should_run = 0
}
});
} else {
$('.key_field').filter(function() {
if (this.value === "") {
alert('Please Fill In Or Remove The Blank Fields First')
e.preventDefault();
should_run = 0
}
});
}
if (should_run != 0) {
if (of_val_field.length === 1) {
$('.val_field:not(:first)').filter(function() {
if (this.value === "") {
alert('Please Fill In Or Remove The Blank Fields First')
e.preventDefault();
should_run = 0
}
});
} else {
$('.val_field').filter(function() {
if (this.value === "") {
alert('Please Fill In Or Remove The Blank Fields First')
e.preventDefault();
should_run = 0
}
});
}
}
update_config_field_from_key_value_field()
});
}
能否请您帮我重构代码,使其看起来更好?
答案 0 :(得分:0)
将您的代码分成较小的函数。
function emptyHandle(e, should_run) {
alert('Please Fill In Or Remove The Blank Fields First');
e.preventDefault();
should_run = 0;
}
function isEmpty(element) {
return element.val() === "";
}
function before_submit_check_if_has_blank_field() {
$("[type=submit]").click(function (e) {
var of_key_field = $('.key_field')
var of_val_field = $('.val_field')
var should_run = 1
if (of_key_field.length === 1) {
if (isEmpty($('.key_field:not(:first)'))) emptyHandle(e,should_run);
} else {
if (isEmpty($('.key_field'))) emptyHandle(e,should_run);
}
if (!should_run) {
if (of_val_field.length === 1 && isEmpty($('.val_field:not(:first)'))) {
emptyHandle(e,should_run);
} else {
if (isEmpty($('.val_field'))) emptyHandle(e,should_run);
}
}
update_config_field_from_key_value_field()
});
}