通过ajax提交后,清除表单字段不起作用

时间:2017-06-22 14:26:57

标签: javascript jquery html ajax

我试图通过ajax提交后清除表单字段,但它不起作用。有人请帮助检查我的代码,并给我一些很好的建议。以下是我的代码:

jQuery(document).on('click', '.um-message-send:not(.disabled)',function(e){

    e.preventDefault();

    jQuery('.um-message-send:visible').addClass('disabled');

    var message_to = jQuery('.um-message-body:visible').attr('data-message_to');

    var content = jQuery('.um-message-textarea textarea:visible').val();



    jQuery.ajax({

        url: um_scripts.ajaxurl,

        type: 'post',

        dataType: 'json',

        data: { action: 'um_messaging_send', message_to: message_to, content: content },



        success: function(data){



            jQuery('.um-message-textarea textarea:visible').val('');

            jQuery('.um-message-body:visible').find('.um-message-ajax:visible').html( data.messages );



            if ( data.limit_hit ) {

                jQuery('.um-message-footer:visible').html( jQuery('.um-message-footer:visible').attr('data-limit_hit') );

            }



            jQuery('.um-popup-autogrow:visible').mCustomScrollbar('update').mCustomScrollbar("scrollTo", "bottom",{ scrollInertia:0});



        }

    });



    return false;

});

HTML

<div class="um-message-textarea"> 

    <?php echo $this->emoji(); ?>

    <textarea name="um_message_text" id="um_message_text" data-maxchar="<?php echo $limit; ?>" placeholder="<?php _e('Type your message...','um-messaging'); ?>"></textarea>

</div>
<div class="um-message-buttons">

    <span class="um-message-limit"><?php echo $limit; ?></span>

    <a href="#" class="um-message-send disabled"><i class="um-faicon-envelope-o"></i><?php _e('Send message','um-messaging'); ?></a>

</div>

1 个答案:

答案 0 :(得分:0)

答案只是将success替换为complete ......

success: function(data) {
  // [...]

=&GT;

complete: function(data) {
  // [...]