【问题标题】:In JavaScript code combining two functions not working, but in single function works fine在 JavaScript 代码中结合两个函数不起作用,但在单个函数中工作正常
【发布时间】:2020-10-01 06:36:48
【问题描述】:

CS50 WEB的project3邮件

当我使用单一功能时,它可以正常工作

function compose_email() {

  // Show compose view and hide other views
  document.querySelector('#read-view').style.display = 'none';
  document.querySelector('#emails-view').style.display = 'none';
  document.querySelector('#compose-view').style.display = 'block';

  // Clear out composition fields
  document.querySelector('#compose-recipients').value = '';
  document.querySelector('#compose-subject').value = '';
  document.querySelector('#compose-body').value = '';

  document.querySelector('#compose-form').onsubmit = function() {

    let recipient = document.querySelector('#compose-recipients');
    let subject = document.querySelector('#compose-subject');
    let body = document.querySelector('#compose-body');
  
    fetch('/emails', {
      method: 'POST',
      body: JSON.stringify({
        recipients: recipient.value,
        subject: subject.value,
        body: body.value,
      })
    })
    .then(response => response.json())
    .then(result => {
      console.log(result);
    });
    load_mailbox('sent')
    return false;
  };
};

但是当我把它分成两个函数时它不会加载load_mailbox('sent')

function compose_email() {

  // Show compose view and hide other views
  document.querySelector('#read-view').style.display = 'none';
  document.querySelector('#emails-view').style.display = 'none';
  document.querySelector('#compose-view').style.display = 'block';

  // Clear out composition fields
  document.querySelector('#compose-recipients').value = '';
  document.querySelector('#compose-subject').value = '';
  document.querySelector('#compose-body').value = '';

  document.querySelector('#compose-form').onsubmit = function() {
    send_email();
   
  };
};
function send_email() {
let recipient = document.querySelector('#compose-recipients');
    let subject = document.querySelector('#compose-subject');
    let body = document.querySelector('#compose-body');
  
    fetch('/emails', {
      method: 'POST',
      body: JSON.stringify({
        recipients: recipient.value,
        subject: subject.value,
        body: body.value,
      })
    })
    .then(response => response.json())
    .then(result => {
      console.log(result);
    });
    load_mailbox('sent')
    return false;
};

【问题讨论】:

  • 你说的不加载是什么意思?您是否收到错误消息?如果是,请发布。
  • 不应该在接收响应的 then 函数中调用 load_mailbox('sent') 函数吗?否则异步调用将执行,在它完成之前你调用 load_mailbox 当然还没有任何数据,因为异步调用还没有返回
  • 为什么是 python/django/cs50 标签?请使用仅与问题相关的标签标记问题

标签: javascript python-3.x django cs50


【解决方案1】:

调用者忽略了send_email 中的return false

让调用者返回该结果,以防止默认行为。

document.querySelector('#compose-form').onsubmit = function() {
    return send_email();
//   ^^^
};

或者只是将send_email 指定为处理程序。确保您不要包含() 呼叫运算符。

document.querySelector('#compose-form').onsubmit = send_email;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多