[英]In JavaScript code combining two functions not working, but in single function works fine
CS50 WEB's project3 mail CS50 WEB的project3邮箱
When I use single funciton it works properly which is当我使用单个功能时,它可以正常工作,这是
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;
};
};
But when I split it into two function it doesn't load load_mailbox('sent')
但是当我把它分成两个函数时,它不会加载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;
};
Your return false
in send_email
is being ignored by the caller.您在send_email
return false
被调用者忽略。
Have the caller return that result, so that it will prevent the default behavior.让调用者返回该结果,以防止默认行为。
document.querySelector('#compose-form').onsubmit = function() {
return send_email();
// ^^^
};
Or just assign send_email
as the handler.或者只是将send_email
指定为处理程序。 Be sure you do not include the ()
call operator.确保不包括()
调用运算符。
document.querySelector('#compose-form').onsubmit = send_email;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.