简体   繁体   English

发送请求后ajaxform jQuery插件重置表格

[英]ajaxform jQuery plugin reset form after sending request

Before i begin I believe this is not a dublicate at first. 在我开始之前,我相信这并不是一开始的复制。

Here's my problem: 这是我的问题:

I have a chatroom php script that i use ajaxForm jQuery plugin in order to send new messages without reloading the whole page. 我有一个聊天室php脚本,我使用ajaxForm jQuery插件来发送新消息而无需重新加载整个页面。

The fact is that posting takes about 1-2 secs with times: eg 事实是发布时间大约需要1-2秒:例如

Blocking:6ms 封闭:6ms的

Sending:1ms 发送:1毫秒

Waiting:1.15 sec 等待中:1.15秒

Recieving 1ms 接收1ms

$('#myform').ajaxForm(function() {
    //clear form after this
    $('#myform').clearForm();
});

The form is cleared after te recieving time,meaning it takes about 1.20sec to clear each message i send... and it seems like the chat lags for a bit. 接收时间过后,表格将被清除,这意味着清除我发送的每条消息大约需要1.20秒...而且聊天似乎有点滞后。

I am trying to figure out a way to clear the form after the Sending part,without callback or something. 我试图找出一种方法来清除发送部分之后的表单,而没有回调或其他东西。

If ajaxForm is working fine, you can do: 如果ajaxForm运行正常,则可以执行以下操作:

$('#myform').ajaxForm({
    beforeSubmit:  function() {
       $('#myform').clearForm();    //Call the reset before the ajax call starts
    },
    success: function(data){
       //Your normal callback here
    }
});

(If $('#myform').clearForm() didn't work use $('#myform')[0].reset(); ) (如果$('#myform').clearForm()无法正常工作,请使用$('#myform')[0].reset();

Hope this helps. 希望这可以帮助。 Cheers 干杯

$('#mydiv').ajaxForm(function() {
   //...
    $("#mydiv")[0].reset();
});

I've never used ajaxForm , but just using the browser's reset() function on the <form> element should suffice (I'm assuming that ajaxForm returns a jQuery instance): 我从未使用过ajaxForm ,但是仅在<form>元素上使用浏览器的reset()函数就足够了(我假设ajaxForm返回jQuery实例):

$('#myform').ajaxForm(function() {
    this.reset();
});

If the above throws a has no method reset() error, try this: 如果以上has no method reset()抛出一个has no method reset()错误,请尝试以下操作:

$('#myform').ajaxForm(function() {
    $('#myform')[0].reset();
});

If you have problems with ajaxForm . 如果您对ajaxForm有问题。 You could avoid ajaxForm with the following: 您可以通过以下方式避免使用ajaxForm

$(".ajaxform").submit(function(event){
   var $form = $(this);
   event.preventDefault();
   $form.get(0).reset();   //Call the js reset method before ajax starts
    $.ajax({
        url: $form.attr("action"),
        type: $form.attr("method"),
        data: $form.serialize(),
        success: function(data){
           //your code when response arrived here
        }
    });
});

Hope this helps. 希望这可以帮助。 Cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM