简体   繁体   English

如何在页面加载时提交ajax表单

[英]How to submit ajax form on page load

I want my page to send ajax on page load, but as I'm really new, I tried to use someone's else code. 我希望我的页面在页面加载时发送ajax,但由于我真的很新,我试图使用别人的其他代码。

I wanted you to help me know why it's not working. 我希望你能帮助我知道它为什么不起作用。 Note: I get Uncaught TypeError: Object #<HTMLFormElement> has no method 'ajaxSubmit' 注意:我得到Uncaught TypeError: Object #<HTMLFormElement> has no method 'ajaxSubmit'

Here's the code: 这是代码:

{% if my_data %}
  <form id="data-form-1" action="/dta/import/" method="post">
    {% csrf_token %}
    <input type="hidden" name="current_data" value="{{ my_data }}" />
  </form>
{% endif %}

And here's the script: 这是脚本:

<script>document.getElementById('data-form-1').ajaxSubmit({
        dataType : 'json',
        success : function(response, statusText, xhr){
          if(response.result.toLowerCase() == 'ok')
          {
            $(".list-empty").remove();
            addDataArray(response.data);
            btnStatus('success');
          }
          else
            btnStatus('error');
        },
        error : function(xhr, statusText, errorThrown){
          btnStatus('error');
        }
      });
</script>

Hope you can help me. 希望您能够帮助我。

Note 2: I don't use PHP, just js and HTML 注2:我不使用PHP,只使用js和HTML

The ajaxSubmit function is jQuery, not a regular JavaScript function on a form, so you need a jQuery object. ajaxSubmit函数是jQuery,而不是表单上的常规JavaScript函数,因此您需要一个jQuery对象。 Change 更改

document.getElementById('data-form-1').ajaxSubmit(...

to

$('#data-form-1').ajaxSubmit(...

document.getElementById('data-form-1') is not a Jquery object. document.getElementById('data-form-1')不是Jquery对象。 ajaxSubmit can be invoked only on converting the same into a jquery object using $('#data-form-1') 只有在使用$('#data-form-1')将它转换为jquery对象时才能调用ajaxSubmit

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

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