简体   繁体   English

在提交按钮上注册单击事件时,表单输入中的必需属性不起作用

[英]Required property in form inputs not working when registering a click event on submit button

I have a Django form and it has inputs with the required property.我有一个 Django 表单,它具有所需属性的输入。 I also have a DWobject inside the form which should have a scanned image before submitting the form.我在表单中还有一个 DWobject,在提交表单之前应该有一个扫描图像。 I want to validate this DWobjec before submitting the form but when I do that with "click event" on the submit button it validates only that object and ignored the other required fields.我想在提交表单之前验证这个 DWobjec,但是当我在提交按钮上使用“单击事件”进行验证时,它仅验证该对象并忽略其他必填字段。 when I tried "on submit" event it checks the required inputs only and ignores the DWobject.当我尝试“提交”事件时,它只检查所需的输入并忽略 DWobject。

Does anyone know how to to keep the required property working while validating the object at the same time?有谁知道如何在验证对象的同时保持所需的属性工作?

This is my Javascript code:这是我的 Javascript 代码:

// validating scan object 
    $("#scan_submit").on("click", function (event) {

        event.preventDefault ? event.preventDefault() : event.returnValue = false;

        if (DWObject.HowManyImagesInBuffer == 0) {
            $('#error_if_empty').text('Please scan a file ').css('color', 'red');
        }

        else {

            $('#error_if_empty').text(' ');
            $("#scan_form").submit();
        }
    });

This is my HTML form:这是我的 HTML 表单:

           
  <form method="post" action="{% url 'cu:scan-page' pk=cu.id  %}" id="scan_form">   
   <div class="modal-body">
   {% csrf_token %}
 <input id="id_filename" name="filename" type="text" class="form-control" required>

   {{ User_attachment_form.type  }}
   {{ User_attachment_form.level }}

  <button type="button" class="btn scan-attachment-action-button"onclick="AcquireImage();">
  <img src="{% static 'images/scan_icon.svg' %}"></button>

  <button type="button" class="btn scan-attachment-action-button"onclick="ShowFileEditor();"> 
  <img src="{% static 'images/edit_icon.svg' %}"> </button>                
 <span id="error_if_empty"></span> 
<div id="dwtcontrolContainer"></div>

<div class="modal-footer">
<button type="submit" class="btn btn-primary" id="scan_submit">  </button>  </div> </form>

I found out the problem, I was registering "on submit" event on the submit button id not the form id我发现了问题,我在提交按钮 id 而不是表单 id 上注册了“提交时”事件

when I replace it with the form id it works!当我用表单 id 替换它时,它起作用了!

this is the code:这是代码:

 // validating scan object 
    $("#scan_form").on("submit", function (event) {
        console.log('on submit')

        if (DWObject.HowManyImagesInBuffer == 0) {
            $('#error_if_empty').text('Please scan a file ').css('color', 'red');
            console.log('images = 0')
            return false;
        }

        else {
            $('#error_if_empty').text(' ');
            return true;
        }
    });

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

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