【问题标题】:How to reset a form AFTER its submission如何在提交后重置表单
【发布时间】:2019-03-14 01:32:20
【问题描述】:

以下代码在用户提交表单时运行。在检查提交的表单数量后(用户只能提交 5 次),表单会记录在服务器端的 google sheet 中。

function validateForm(form){   
    if(count>=MAX){
        var msg={
            title:"Idea limit reached",
            body:"You can propose a maximum of "+MAX+" ideas for this theme",
            btn:"OK"
        }
        screenMessage(msg)
    }
    else{
        var msg={
            title:"Idea recorded",
            body:"You can still submit "+(5-count-1)+" ideas",
            btn:"OK"
        }
        google.script.run.withSuccessHandler(successMessage(msg), resetForm()).recordIdea(form)    
        count++
    }
    return false;  
  }
function resetForm(){
    $('#title').val("")
    $('#description').val("")
    $('input[name="tags"]').val("")
    $('.creation-tags').empty()
    $('.suggested-tags').empty();
}

我的问题是查数据库的时候,我输入的信息是空的!这意味着“resetForm”函数实际上是在表单提交之前调用的。

有没有办法确保只有在用户输入的值被成功提交后才调用resetForm函数?

我尝试将函数嵌套在 successMessage 函数中,但没有得到肯定的结果

【问题讨论】:

    标签: javascript html forms google-apps-script


    【解决方案1】:

    这行代码是马上调用resetForm(),而不是设置回调函数:

    google.script.run.withSuccessHandler(successMessage(msg), resetForm()).recordIdea(form)
    

    应该是这样的:

    google.script.run.withSuccessHandler(function(){successMessage(msg), resetForm()}).recordIdea(form)
    

    【讨论】:

    • 长话短说,为了更好地解释这一点,您需要“withSuccessHandler”中的“回调函数”。相反,您的代码所做的基本上是注入“successMessage”和“resetForm”的返回值,因为它们会在 Javascript 遇到它们时立即运行。如果您需要更好地了解回调函数,可以找到各种有关回调函数的文章和帖子。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-03
    • 2018-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多