简体   繁体   English

如何在 django 模板中使用 javascript

[英]How to use javascript in django template

I am using javascript for validating a form in django template, but it's not working,我正在使用 javascript 验证 django 模板中的表单,但它不起作用,

the DOM events are not fetching the values. DOM 事件没有获取值。 even if i give valid input to form the console says empty string即使我提供有效输入以形成控制台说空字符串

HTML snippet HTML 片段

<form onsubmit="return validate_hotel()" action="confirm_hotel/{{hotel_name}}" method="post">
    {% csrf_token %}
    <div id="left">
        <label>Check in date</label>
        <input id="checkin" type="date" autocomplete="off" name="checkin" > <br><br>
        <label>Number of guests</label><br>
        <select autocomplete="off"  name="guests" id="guests">
            <option value="1">1</option>
            <option value="2">2</option>
            <option value="3">3</option>                                       
        </select><br><br>
        <label>Username</label>
        <input id="HotelUsername" type="text"  name="username" autocomplete="off">
    </div>

    <div id="right">
        <label>Check out date</label>
        <input id="checkout" type="date" autocomplete="off" name="checkout"> <br><br>
        <label>Number of rooms</label><br>
        <select autocomplete="off">
            <option value="1">1</option>                                        
            <option value="2">2</option><br>
        </select><br><br>
        <label> password</label>
        <input id="password" type="password" autocomplete="off" name="password" >
     </div>
     <br><label id="hotel_error_message"> {{error_message}} </label><br>
     <button type="submit">Confirm booking</button>
</form>

here is the javascript function i am using to validate things.这是我用来验证事物的 javascript function。 i am using document.getElementById() method to fetch the input values.我正在使用 document.getElementById() 方法来获取输入值。

function validate_hotel()
{
    var hotel_username = document.getElementById("HotelUsername").value;
    var checkin = document.getElementById("checkin").value;
    var checkout = document.getElementById("checkout").value;
    var hotel_password = document.getElementById("password").value;
    var date_regx = /^(19|20)\d\d([- /.])(0[1-9]|1[012])\2(0[1-9]|[12][0-9]|3[01])$/
    var username_regx = /^[a-zA-Z0-9_$.@]+$/
    var password_regx = /^(?=.*\d).{4,12}$/
    var valid = true;
    console.log(hotel_username, checkin, checkout, hotel_password);
    if (! username_regx.test(hotel_username))
    {
        valid = false;
    }

    if (! password_regx.test(hotel_password))
    {
        valid = false;
    }

    if (! date_regx.test(checkin))
    {
        valid = false;
    }

    if (! date_regx.test(checkout))
    {
        valid = false;
    }

    if (!valid)
    {
        document.getElementById("hotel_error_message").innerHTML = "Invalid inputs";
    }

    return valid;
};

Javascript code is executed by the browser (client side) and It is not related to backend (django etc...). Javascript 代码由浏览器(客户端)执行,与后端(django 等)无关。 Your code has no problem and works correctly.您的代码没有问题并且可以正常工作。

To check javascript code I'm using Firefox debugger .要检查 javascript 代码,我正在使用Firefox 调试器 It is possible to create break points in your javascript code and watch the variables to check if they get value or not.可以在 javascript 代码中创建断点并观察变量以检查它们是否获得值。

  • In Firefox browser, open debugger.在 Firefox 浏览器中,打开调试器。
  • Find the file that contains javascript code找到包含javascript代码的文件
  • create some break points on some lines在某些行上创建一些断点
  • execute your code (submit the form)执行你的代码(提交表单)
  • and then by hovering the mouse on your variables check if they get value or not.然后将鼠标悬停在变量上,检查它们是否有价值。 you can also add some watch to check variables.您还可以添加一些手表来检查变量。
  • when your code is stopped at break points, checking the value of variables is also available in console.当您的代码在断点处停止时,控制台中也可以检查变量的值。

Anyway, the code works correctly check if you have some elements with same name or something like this.无论如何,代码可以正常工作,检查您是否有一些具有相同名称或类似名称的元素。

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

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