Javascript 如何避免表单中的文本字段清除onClick?

Javascript 如何避免表单中的文本字段清除onClick?,javascript,jquery,html,Javascript,Jquery,Html,我有一个带有4个文本字段的简单表单。当用户单击保存按钮保存这些字段的内容时,文本字段将自动清除。我如何避免这种情况 这是我的函数,它在单击时运行onclick: function saveProblem() { var location = document.getElementById("txtinput_location").value; var description = document.getElementById("description").value;

我有一个带有4个文本字段的简单表单。当用户单击保存按钮保存这些字段的内容时,文本字段将自动清除。我如何避免这种情况

这是我的函数,它在单击时运行
onclick

function saveProblem() {    
    var location = document.getElementById("txtinput_location").value;
    var description = document.getElementById("description").value;
    var importance = document.getElementById("importance").value;
    var type = document.getElementById("problemtype").value;        
    var datetime = new Date();
    var date = "Dato: " + datetime.toLocaleDateString() + " KL: " + datetime.getHours() + ":" + datetime.getMinutes();

    if (location === "" || description === "" || description === "" || importance === "") {
        alert("\nALLE FELTER SKAL UDFYLDES!");
    } else {
        problem_array.push(new Problem(location, description, importance, type, date));
    }

    console.log("OBJECT: " + location + " " + importance + " " + type + " " + description + " " + date);
    console.log("Array lenght: " + problem_array.length);
}

您确定您的按钮实际上没有提交表单吗?忘记放置
type=“button”
将使
表单
标签中的按钮充当提交按钮并发送表单。由于此代码中没有任何内容将字段的值设置为“”,因此我猜这是您真正的问题尝试过这个吗?@delighteddod谢谢你的努力!我按照你的建议做了,并在我的按钮上添加了类型。你确定你的按钮实际上没有提交你的表单吗?忘记放置
type=“button”
将使
表单
标签中的按钮充当提交按钮并发送表单。由于此代码中没有任何内容将字段的值设置为“”,因此我猜这是您真正的问题尝试过这个吗?@delighteddod谢谢你的努力!我照你的建议做了,把字按在按钮上。