weixin_33744141 2016-04-15 16:30 采纳率: 0%
浏览 31

Ajax回调未执行

I have a form page that I intend submitting using Ajax. My plan is to 1. Check if email already exists using Ajax 2. Check if passwords match 3. If it does, switch to another "screen" and 4. Submit the form using Ajax

I'm confused because the validate function does not run. It neither switches screens not alerts when passwords do not match. As such, the form does not get submitted either. My code goes below

$('form input:last-child').click(function(e){
    e.preventDefault();
    var allFields = e.target.parentNode;

    function validate () {
        if (allFields.elements[3].value !== allFields.elements[4].value) {
            return false; // If they don't match, return false
        } else {
            $('#form-div form').css('left', '-70%');
            $('#confirm p').css('margin-left', '-12%'); // else switch screens
        }
    }
    if (validate != false) {
        $('#hidden').load("server_script.php"); // `hidden` is a hidden div somewhere on the page
    } else 
        alert ("Passwords do not match");
});

I'm thinking, if they don't match, the rest of the event listener won't run since the false terminates the function from that point on. So I tried making an instance of the validate function outside the event listener and calling it inside the click function but it won't parse because of dependency variables so I'm not sure how to go about this.

UPDATE Associated HTML attached. (Bonus: the regex pattern does not match 2 or more letters)

<div id=form-div>
<form method=POST action="" >
First Name: <br> <input type=text name=first_name pattern="[a-z]{2,}" required /> <br> <br>
Last Name: <br> <input type=text name=last_name pattern="[a-z]{2,}" required /> <br> <br>
Email: <span id=emailReport></span> <br> <input type=email name=email id=email required /> <br> <br>
Password: <br> <input type=password name=password required pattern=".{6,}" title="Password must be six or more characters" /> <br> <br>
Confirm password: <br> <input type=password name=password required /> <br> <br>
<input type=hidden name=sign_up_date />
<input type=submit value='sign up' />
</form>

<div id=confirm> <p>Some text</p> </div>
</div>

</div>
  • 写回答

3条回答 默认 最新

  • weixin_33743661 2016-04-15 16:34
    关注

    The validate function is not being called. Why not simply remove the function definition so the code is part of the click function?

    评论

报告相同问题?

悬赏问题

  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败