0

伙计们,我这里有一个情况。我试图验证来自用户的输入,如果输入是!= "",它将在 if 内执行什么操作,如果是 else,它将在 else 内执行操作。除了一件事之外,验证非常有效。尽管条件为假,但它仍会进入 if 条件并使用此代码刷新整个系统$(location).attr('href','account_setting_registrar.php');

你能帮助我吗?

        $(document).ready(function (){
         var username ;
         var eadd;

$('#change_usrname_button').click(function (){
    username = $('#new_chnge_username').val();      

            if(username != "" ){
                    $.ajax({
                        type: 'POST',
                        url: 'account_setting_registrar_get.php',
                        dataType: 'json',
                        data:'username='+username,
                        success: function (data){


                                   if(data.error == false){

                                    alert("your username is successfuly changed");

                                       $(location).attr('href','account_setting_registrar.php');
                                   }
                                   else{
                                    alert("update error");
                                 }
                       }  
                    });

                }

                else{
                   alert("please put the new username");
                 }


});



            $('#change_eadd_button').click(function (){


                eadd = $('#new_chnge_eadd').val();


                if(eadd != "" ){
                        $.ajax({
                            type: 'POST',
                            url: 'account_setting_registrar_get.php',
                            dataType: 'json',
                            data:'emailadd='+eadd,
                            success: function (data){

                               if(data.error == false){

                                alert("your email address is successfuly changed");
                                $(location).attr('href','account_setting_registrar.php');
                               }

                               else{
                                alert("update error");
                               }

                             }
                        });
                 }

                 else{
                   alert("please put the new username");
                 }



        });

            });
4

3 回答 3

1

我认为您的验证应该更彻底...

null您只检查空字符串,但是如果用户名是或也是,您也不希望它有效undefined

您的验证应该如下:

if (username) {
 // ajax
}

这将检查以下内容:

  • 空值
  • 不明确的
  • 空字符串 ("")
  • 0
  • 错误的
于 2012-09-28T04:30:53.160 回答
0

You should use data.error=='false' because the response you may getting is a string in json format.

于 2012-09-28T05:25:10.133 回答
-1
       $(document).ready(function (){
         var username ;
         var eadd;

$('#change_usrname_button').click(function (){
    username = $('#new_chnge_username').val();      

            if(username){
                    $.ajax({
                        type: 'POST',
                        url: 'account_setting_registrar_get.php',
                        dataType: 'json',
                        data:'username='+username,
                        success: function (data){


                                   if(data.error == true){

                                    alert("your username is successfuly changed");

                                       $(location).attr('href','account_setting_registrar.php');
                                   }
                                   else{
                                    alert("update error");
                                 }
                       }  
                    });

                }

                else{
                   alert("please put the new username");
                 }


});



            $('#change_eadd_button').click(function (){


                eadd = $('#new_chnge_eadd').val();


                if(eadd){
                        $.ajax({
                            type: 'POST',
                            url: 'account_setting_registrar_get.php',
                            dataType: 'json',
                            data:'emailadd='+eadd,
                            success: function (data){

                               if(data.error == true){

                                alert("your email address is successfuly changed");
                                $(location).attr('href','account_setting_registrar.php');
                               }

                               else{
                                alert("update error");
                               }

                             }
                        });
                 }

                 else{
                   alert("please put the new username");
                 }



        });

            });

你可以试试这个先生..

于 2012-09-28T04:35:54.853 回答