有人能优化这个代码吗?
任何人都可以优化代码,减少使用for循环的if using条件吗?我正在尝试用laravel通过ajax提交表单数据。当验证失败时,我试图在输入字段前面打印错误。代码运行良好,但如果是其他条件,则会有太多的代码。
$("#form").submit(function(event){
event.preventDefault();
$.ajax({
url: $("#reseller_form").attr("action"),
method:'POST',
dataType:'json',
data:$(this).serialize(),
success:function(response){
console.log(response);
},
error:function(data){
if(data.responseJSON.name){
$('#reseller_name_error').html(data.responseJSON.name).css('color','red');
}else{
$('#reseller_name_error').html('');
}
if(data.responseJSON.phone){
$('#reseller_phone_error').html(data.responseJSON.phone).css('color','red');
}else{
$('#reseller_phone_error').html('');
}
if(data.responseJSON.email){
$('#reseller_email_error').html(data.responseJSON.email).css('color','red');
}else{
$('#reseller_email_error').html('');
}
if(data.responseJSON.state){
$('#reseller_state_error').html(data.responseJSON.state).css('color','red');
}else{
$('#reseller_state_error').html('');
}
if(data.responseJSON.district){
$('#reseller_district_error').html(data.responseJSON.district).css('color','red');
}else{
$('#reseller_district_error').html('');
}
if(data.responseJSON.city){
$('#reseller_city_error').html(data.responseJSON.city).css('color','red');
}else{
$('#reseller_city_error').html('');
}
if(data.responseJSON.address){
$('#reseller_address_error').html(data.responseJSON.address).css('color','red');
}else{
$('#reseller_address_error').html('');
}
},
});
});这就是我想要做的
$("#reseller_form").submit(function(event){
event.preventDefault();
$.ajax({
url: $("#reseller_form").attr("action"),
method:'POST',
dataType:'json',
data:$(this).serialize(),
success:function(response){
console.log(response);
},
error:function(data){
var error_data = data.responseJSON;
var x;
for (x in error_data) {
if(x){
$('#reseller_'+ x +'_error').html(error_data[x]).css('color','red');
}else{
$('#reseller_'+ x +'_error').html('');
}
}
//console.log(text);
},
});
//end of ajax
});
//end of reseller form submit发布于 2018-03-08 10:11:49
您可以使用该代码修复多个请求验证失败。
$("#reseller_form").submit(function(event){
event.preventDefault();
$.ajax({
url: $("#reseller_form").attr("action"),
method:'POST',
dataType:'json',
data:$(this).serialize(),
success:function(response){
console.log(response);
},
error:function(data){
var error_data = data.responseJSON;
var object = {"name":"","email":"","phone":""};
var x;
for (x in error_data) {
$('#reseller_'+ x +'_error').html(error_data[x]).css('color','red');
delete object[x];
}
for(x in object)
{
$('#reseller_'+ x +'_error').html('');
}
},
});
//end of ajax
});
//end of reseller form submit发布于 2018-03-07 11:01:20
var data = {}
data['responseJSON'] = {} //your data
var d = {
'name': '#reseller_name_error',
'phone': '#reseller_phone_error',
'email': '#reseller_email_error',
'state': '#reseller_state_error',
'district': '#reseller_district_error',
'city': '#reseller_city_error',
'address': '#reseller_address_error'
}
for (k in d) {
if (k in data.responseJSON) {
$(d[k]).html(data.responseJSON.k).css('color', 'red');
} else {
$(d[k]).html('')
}
}<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
https://stackoverflow.com/questions/49148995
复制相似问题