简体   繁体   English

Django / JQuery:无法使用JQuery停止Django表单提交(preventDefault()等无法正常工作)

[英]Django/JQuery: can't stop Django form submission with JQuery (preventDefault(), etc. not working)

I'm trying to do some simple client-side validation because my form is really simple and just needs to know if all of its fields have been filled out. 我正在尝试进行一些简单的客户端验证,因为我的表单非常简单,只需要知道其所有字段是否都已填写即可。

Here is the relevant part of the template: 这是模板的相关部分:

{% block content %}
 <form id="location_form" action="{% url "mountain_results" %}" method="GET">
    <h2>Enter your location</h2>
    <input id="location_autocomplete" placeholder="Enter your address" name="address"
    onFocus="geoLocate()" type="text"></input>

    <h2>Enter your maximum drive time</h2>
    <input name="drive_time"
    placeholder="Enter your maximum drive time (hours)" type="text"></input>

    <h2>Are you shredding today or tomorrow?</h2>
    <select name="shred_day">
        <option value="today">Today</option>
        <option value="tomorrow">Tomorrow</option>
    </select>
    <br>
    <button type="submit">Find me snow</button>
</form>
{% endblock content %}

Here is the relevant part of the JS: 这是JS的相关部分:

$('#location_form').on('submit', function(event) {
    alert('dude you totally clicked');
    event.preventDefault();
    return false;
});

Interestingly, the alert doesn't even fire, so it feels like the event handler isn't even being triggered. 有趣的是,警报甚至没有触发,因此感觉事件处理程序甚至没有被触发。 Any thoughts? 有什么想法吗? As you can see, I tried both commonly used methods of preventing the default event from firing to no avail. 如您所见,我尝试了两种防止默认事件触发均无济于事的常用方法。

You can use onsubmit event handler to validate the form.I have kep the action attribute empty for demo. 您可以使用onsubmit事件处理程序来验证表单。对于演示,我保留action属性为空。 You can add it along with onsubmit . 您可以将其与onsubmit一起添加。 Also is there any specific reason to for GET method. GET方法也没有任何特定的原因。 Here is a snippet. 这是一个片段。

HTML HTML

<form id="location_form" action="" method="POST" onsubmit="return _validateForm()">
    <h2>Enter your location</h2>
    <input id="location_autocomplete" placeholder="Enter your address" name="address"
    onFocus="geoLocate()" type="text"></input>

    <h2>Enter your maximum drive time</h2>
    <input name="drive_time"
    placeholder="Enter your maximum drive time (hours)" type="text"></input>

    <h2>Are you shredding today or tomorrow?</h2>
    <select name="shred_day">
        <option value="today">Today</option>
        <option value="tomorrow">Tomorrow</option>
    </select>
    <br>
    <button type="submit">Find me snow</button>
</form>

JS JS

function _validateForm(){
 alert('dude you totally clicked');
}

Here is a DEMO .Hope this will be helpful 这是一个演示。希望这会有所帮助

Here is the another approach. 这是另一种方法。 Change 更改

<button type="submit">Find me snow</button>

With

<button id="submit_form">Find me snow</button>

and add following JavaScript code 并添加以下JavaScript代码

$('#submit_form').click(function(){
    if(for_is_valid()){
       $('#location_form').submit();
    }
    else{
       alert('Your message');
    }
});

Hope this will work. 希望这会起作用。

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

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