gpt4 book ai didi

javascript - 单击提交按钮后如何重新加载表单?

转载 作者:行者123 更新时间:2023-12-02 22:38:12 25 4
gpt4 key购买 nike

我对 JavaScript 非常陌生,不知道如何解决这个问题。现在,如果您单击“提交”按钮,您将收到一条警报消息,但当您关闭它时,所有条目仍然存在。

function contactform(){
var fname = document.getElementById("fnameinput");
var lname = document.getElementById("lnameinput");
var email = document.getElementById("emailinput");
var phone = document.getElementById("phoneinput");


fname.value && lname.value && (email.value || phone.value) ? alert
("Thank you, " + fname.value + " " + lname.value + "\n"
+ "We will get back to you shortly!") : alert ("Please fill in all fields.");
}


document.getElementById("submit").addEventListener("click", contactform, false,);

我希望在单击提交按钮后重新加载整个页面。

最佳答案

要重新加载页面,您只需将 window.location 设置为 ''

你可以这样做:

window.location = ''

或者像这样:

window.location.assign('')

完整的函数如下所示:

function contactform(){
var fname = document.getElementById("fnameinput");
var lname = document.getElementById("lnameinput");
var email = document.getElementById("emailinput");
var phone = document.getElementById("phoneinput");

if(fname.value && lname.value && (email.value || phone.value)) {
alert("Thank you, " + fname.value + " " + lname.value + "\n" + "We will get back to you shortly!");
window.location.assign('');
} else alert("Please fill in all fields.");
}

或者,进行一些重构:

function contactform(){
const fname = document.getElementById("fnameinput");
const lname = document.getElementById("lnameinput");

if(fname.value && lname.value && (document.getElementById("emailinput").value || document.getElementById("phoneinput").value)) {
alert(`Thank you, ${fname.value} ${lname.value}\nWe will get back to you shortly!`);
window.location.assign('');
} else alert("Please fill in all fields.");
}

关于javascript - 单击提交按钮后如何重新加载表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58674617/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com