简体   繁体   English

做一个 jQuery Ajax 提交,但它不起作用

[英]Doing a jQuery Ajax submit, but it doesn't work

I am trying to do a ajax post request, to also read a JSON response from the URL, but if I click the button nothing happens.我正在尝试执行 ajax 发布请求,同时从 URL 读取 JSON 响应,但是如果我单击按钮,则什么也没有发生。 What could be wrong with my HTML code or is it the Javascript?我的 HTML 代码可能有什么问题,还是 Javascript? The function submitRegister is empty at the moment.函数submitRegister目前为空。

Javascript: Javascript:

$('#registerForm').submit(function(event){
    event.preventDefault();
    submitRegister();
});

function submitRegister(){
    console.log("Test");
}

HTML HTML

<div id="RegisterM" class="modal fade" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">Register</h4>
            </div>
            <div id='notification'>
            </div>
            <div class="modal-body">
                <form role="form" id="registerForm">
                    <div class="form-group">
                        <label for="Username">Username:</label>
                        <input type="Username" class="form-control" id="username">
                    </div>
                    <div class="form-group">
                        <label for="email">Email address:</label>
                        <input type="email" class="form-control" id="email">
                    </div>
                    <div class="form-group">
                        <label for="pwd">Password:</label>
                        <input type="password" class="form-control" id="pwd">
                    </div>
                    <div class="form-group">
                        <label for="confirmpwd">Confirm Password:</label>
                        <input type="confirmpwd" class="form-control" id="confirmpwd">
                    </div>
                    <button type="button" class="btn btn-default" id='register-submit'>Register</button>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>

    </div>
</div>

Your button needs to be of type "submit" :您的按钮需要是"submit"类型:

<button type="submit" class="btn btn-default" id='register-submit'>Register</button>

Per the jQuery docs:根据 jQuery 文档:

The submit event is sent to an element when the user is attempting to submit a form.当用户尝试提交表单时,将提交事件发送到元素。 It can only be attached to <form> elements.它只能附加到<form>元素。 Forms can be submitted either by clicking an explicit <input type="submit"> , <input type="image"> , or <button type="submit"> , or by pressing Enter when certain form elements have focus.可以通过单击明确的<input type="submit"><input type="image"><button type="submit">或在某些表单元素具有焦点时按 Enter 来<button type="submit">表单。

Why you did such a strange way to send ajax(submit -> prevent submitting -> send ajax)?为什么你用这么奇怪的方式发送ajax(提交-> 阻止提交-> 发送ajax)? Instead of doing this:而不是这样做:

$('#registerForm').submit(function(event){
event.preventDefault();
submitRegister();
});

function submitRegister(){
console.log("Test");
}

Easily create an input轻松创建输入

<input type="button" onclick="submitRegister()" vlaue="ajax" >

And there is many things to cause problem to an ajax request.并且有很多事情会导致 ajax 请求出现问题。 You muse write its code very carefully and go step by step to find the reason of your problem.您必须非常仔细地编写它的代码并一步一步地找到问题的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM