任何人都可以优化代码使用for循环减少if else条件

时间:2018-03-07 10:00:11

标签: jquery ajax optimization

任何人都可以优化此代码 任何人都可以优化代码使用for循环减少if else条件 我试图通过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

2 个答案:

答案 0 :(得分:0)



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>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您可以使用该代码修复多个请求验证失败。

$("#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