dpkpaxhzffixp8426 2015-11-16 17:29
浏览 14
已采纳

在html验证后提交ajax

im having a few problems with an ajax login form when i am clicking submit button its not validating the form before submitting the ajax its just submitting the form where am i going wrong

this is the ajax script

    function wa_login() {
        var wa_username = $('#wa_username').val();
        var wa_password = $('#wa_password').val();
        var datas = 'wa_username=' + wa_username + '&wa_password=' + wa_password;

        $.ajax({
            type: 'POST',
            url: '/limitless/functions.php',
            data: datas
        })
        .done(function (data) {
            $('#info').html(data);
        });
    }

here is the form itself

<form class='form-horizontal form-validate-jquery' action='#' novalidate='novalidate'>
<div class='text-center'>
<div class='icon-object border-slate-300 text-slate-300'><i class='icon-reading'></i></div>
<h5 class='content-group'>Login to your account <small class='display-block'>Enter your credentials below</small></h5>
    </div>

<div class='form-group has-feedback has-feedback-left'>
<input type='text' class='form-control' placeholder='Username' id='wa_username' required='required'>
 <div class='form-control-feedback'>
            <i class='icon-user text-muted'></i>
        </div>
    </div>

    <div class='form-group has-feedback has-feedback-left'>
        <input type='password' class='form-control' placeholder='Password' id='wa_password' required='required'>
        <div class='form-control-feedback'>
            <i class='icon-lock2 text-muted'></i>
        </div>
    </div>

    <div class='form-group'>
        <button type='submit' onclick='wa_login()'   class='btn btn-primary btn-block'>Sign in <i class='icon-circle-right2 position-right'></i></button>
    </div>

    <div class='text-center'>
        <a href='reset'>Forgot password?</a> | <a href='register' >Sign Up</a>
    </div>
    </form>
  • 写回答

2条回答 默认 最新

  • dou12754 2015-11-16 17:42
    关注

    What you need to do is catch the form submit event with jQuery as follows:

    $('#form').on('submit',function(event){
      
      event.preventDefault(); // prevent default behavior
      
      if(formIsValid()){  // Execute validations in formIsValid() function
                          // returning true if valid, false if not
        $.ajax({
                type: 'POST',
                url: '/limitless/functions.php',
                data: $(this).serialize() // automatically serializing form
            })
            .done(function (data) {
                $('#info').html(data);
        });
      } else{
        console.log('Sorry, wrong login credentials');
      }
      
      function formIsValid(){
        // Always returning false to show how submit is not called
        return false;
      }
      
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <!-- Notice that an id is added to your form -->
    <form id="form" class='form-horizontal form-validate-jquery' action='#' novalidate='novalidate'>
    <div class='text-center'>
    <div class='icon-object border-slate-300 text-slate-300'><i class='icon-reading'></i></div>
    <h5 class='content-group'>Login to your account <small class='display-block'>Enter your credentials below</small></h5>
        </div>
    
    <div class='form-group has-feedback has-feedback-left'>
    <input type='text' class='form-control' placeholder='Username' id='wa_username' required='required'>
     <div class='form-control-feedback'>
                <i class='icon-user text-muted'></i>
            </div>
        </div>
    
        <div class='form-group has-feedback has-feedback-left'>
            <input type='password' class='form-control' placeholder='Password' id='wa_password' required='required'>
            <div class='form-control-feedback'>
                <i class='icon-lock2 text-muted'></i>
            </div>
        </div>
    
        <div class='form-group'>
            <button type='submit' class='btn btn-primary btn-block'>Sign in <i class='icon-circle-right2 position-right'></i></button>
        </div>
    
        <div class='text-center'>
            <a href='reset'>Forgot password?</a> | <a href='register' >Sign Up</a>
        </div>
    </form>

    Watch the console log, you must see a message alerting about wrong login credentials. Hope it helps!

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 51单片机使lcd显示器开机闪烁预设字符闪烁3次需要加什么代码
  • ¥50 C# 使用DEVMOD设置打印机首选项
  • ¥15 麒麟V10 arm安装gdal
  • ¥15 想用@vueuse 把项目动态改成深色主题,localStorge里面的vueuse-color-scheme一开始就给我改成了dark,不知道什么原因(相关搜索:背景颜色)
  • ¥20 OPENVPN连接问题
  • ¥15 flask实现搜索框访问数据库
  • ¥15 mrk3399刷完安卓11后投屏调试只能显示一个设备
  • ¥100 如何用js写一个游戏云存档
  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题