首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >是否有人可以优化代码,以减少使用for循环的if for条件?

是否有人可以优化代码,以减少使用for循环的if for条件?
EN

Stack Overflow用户
提问于 2018-03-07 10:00:11
回答 2查看 59关注 0票数 2

有人能优化这个代码吗?

任何人都可以优化代码,减少使用for循环的if using条件吗?我正在尝试用laravel通过ajax提交表单数据。当验证失败时,我试图在输入字段前面打印错误。代码运行良好,但如果是其他条件,则会有太多的代码。

代码语言:javascript
复制
$("#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('');
                    }  


                },
            });
    });

这就是我想要做的

代码语言:javascript
复制
 $("#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
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-03-08 10:11:49

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

代码语言:javascript
复制
$("#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
票数 0
EN

Stack Overflow用户

发布于 2018-03-07 11:01:20

代码语言:javascript
复制
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('')
  }

}
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49148995

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档