简体   繁体   English

未调用所需的 function 来满足 javascript 中的条件(提交表单时)

[英]desired function not called on fullfilling the condition in javascript (when form is submitted)

here's my form in html:这是我在 html 中的表格:

<div id="compose-view">
    <h3>New Email</h3>
    <form id="compose-form">
        <div class="form-group">
            From: <input disabled class="form-control" value="{{ request.user.email }}">
        </div>
        <div class="form-group">
            To: <input id="compose-recipients" class="form-control">
        </div>
        <div class="form-group">
            <input class="form-control" id="compose-subject" placeholder="Subject">
        </div>
        <textarea class="form-control" id="compose-body" placeholder="Body"></textarea>
        <input id="compose-button" type="submit" value="SEND" class="btn btn-primary"/>
    </form>
    </div>
{% endblock %}

here are two of my functions in the script file:这是脚本文件中的两个函数:

document.addEventListener(
    "DOMContentLoaded",
    function send_email(){
        const msg = document.querySelector("#message")
        const form = document.querySelector('#compose-form');
        form.addEventListener("submit", () => {
        const recipients = document.querySelector('#compose-recipients').value;
        const subject = document.querySelector('#compose-subject').value;
        const body = document.querySelector('#compose-body').value;
        console.log(recipients);
        //sending a post request for sending email
        fetch('/emails', {
            method: 'POST',
            body: JSON.stringify({
            recipients: recipients,
            subject: subject,
            body: body
            })
         })
         .then(response => response.json())
         .then(result => {
         // Print result
         if (result.status == 201){
         //the mail was sent successfully, load sentbox
            load_mailbox('sent');
         }

         else{
             //display what error occurred while sending the email
             msg.innerHTML = `<div class="alert alert-danger" role="alert">
        ${result.error}
      </div>`;
          }
          console.log(result);
          console.log("message" in result);
          console.log("error" in result);
              })
          .catch(error => {
              // in case an error has occurred
              console.log(error);
              });
           });
           // even if i call the function without any condition it won't do anything
           //load_mailbox('sent');
       });

the function which i am talking about is load_mailbox('sent')我正在谈论的 function 是 load_mailbox('sent')

if (result.status == 201){ //the mail was sent successfully, load sentbox load_mailbox('sent'); if (result.status == 201){ //邮件发送成功,加载sentbox load_mailbox('sent'); } }

but this function isn't called at the end what is going wrong here?但是最后没有调用这个 function 这里出了什么问题?

Try to change to:尝试更改为:

if (parseInt(result.status) === 201)

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

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