提交时清除输入字段

Clear input fields on submit

我试图在用户点击使用 MailChimp 提交后清除我的输入字段,尽管我似乎遇到了问题。

我已经尝试了很多选项来让它工作,但没有任何效果。你能帮帮我吗?

提前致谢。

HTML

<div class="mc-field-group">
    <div class="row">
        <div class="col-sm-6 col-md-6">
            <input type="text" placeholder="Type your name here..." 
                   value="" 
                   name="NOME" class="form-control input-box required pull- 
                   right" id="mce-NOME">
        </div>
        <div class="col-sm-6 col-md-6">
            <input type="text" placeholder="Type your email here..." 
                   value="" name="EMAIL" class="form-control input-box 
                   required pull-left" id="mce-EMAIL">
        </div>
    </div>
</div>
<div class="mc-field-group">
    <div class="row">
        <div class="col-sm-12">
            <textarea limit="1000" placeholder="Type your message here..." 
                      style="height: 100px; width: 500px;" value="" 
                      name="MENSAGEM" class="form-control textarea-box required" 
                      id="mce-MENSAGEM">
            </textarea>
        </div>
    </div>
</div>

JS

$('#mc-embedded-subscribe-form').ajaxChimp({
callback: mailchimpCallback,
url: ""  
});

function mailchimpCallback(resp) {
 if (resp.result === 'success') {
    $('.subscription-success').html('<span class="icon_check_alt2"></span>' + ' Agradecemos por sua mensagem.<br />Entraremos em contato em breve.').fadeIn(1000);
    $('.subscription-error').fadeOut(500);

} else if(resp.result === 'error') {
    $('.subscription-error').html('<span class="icon_close_alt2"></span>' + 'Oops... Houve um erro ao tentar enviar sua mensagem.<br/>Por favor, tente novamente em alguns minutos.').fadeIn(1000);
  }  
}

你可以简单地把它放在你的成功处理程序中,比如

$('.mc-field-group input, .mc-field-group textarea').val('')

应该可以解决问题。

如果表单中有字段,可以在表单上使用重置

$('#formId')[0].reset();
$('.mc-field-group').find('input,textarea').val('')

您可以使用 $(':input').val('');$('textarea').val('');

$("#reset").click(function(){
  $(':input').val('');
  $('textarea').val('');

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="mc-field-group">
    <div class="row">
        <div class="col-sm-6 col-md-6">
            <input type="text" placeholder="Type your name here..." 
                   value="" 
                   name="NOME" class="form-control input-box required pull- 
                   right" id="mce-NOME">
        </div>
        <div class="col-sm-6 col-md-6">
            <input type="text" placeholder="Type your email here..." 
                   value="" name="EMAIL" class="form-control input-box 
                   required pull-left" id="mce-EMAIL">
        </div>
    </div>
</div>
<div class="mc-field-group">
    <div class="row">
        <div class="col-sm-12">
            <textarea limit="1000" placeholder="Type your message here..." 
                      style="height: 100px; width: 500px;" value="" 
                      name="MENSAGEM" class="form-control textarea-box required" 
                      id="mce-MENSAGEM">
            </textarea>
        </div>
    </div>
</div>

<input type="button" id="reset" value="Reset">