简体   繁体   English

加载页面和提交表单时如何提交表单?

[英]How to submit a form when load page and when submit form?

If I have this: 如果我有这个:

 <script language="javascript"> $(document).ready(function() { $('#form').submit(function() { $.ajax({ type: 'POST', url: $(this).attr('action'), data: $(this).serialize(), success: function(data) { $('#result').html(data); } }) return false; }); }) </script> 
 <form name="form" id="form" method="post" action="results.php"> <input type="text" name="date" id="date" value="2014-11-22"> <input name="send" type="submit" value="Submit"> </form> <div id="result"></div> 

Which works perfectly, how can I submit it when the page is loaded? 哪一种效果很好,当页面加载后如何提交?

This should work for you: 这应该为您工作:

(It's submit's the form when the document is ready) (准备好文档后即提交表单)

$(document).ready(function(){
    $("#form").submit();
});

To automatic submit your form use: 要自动提交表单,请使用:

<script language="javascript">
    $(function(){
        $("form").submit();
    });
</script> 

(But be sure your action is not on the same page!) (但是请确保您的操作不在同一页面上!)

EDIT: 编辑:

So your script should look something like this: 因此,您的脚本应如下所示:

<script language="javascript">
    $(document).ready(function() {

        $.ajax({
            type: 'POST',
            url: $(this).attr('action'),
            data: $(this).serialize(),

            success: function(data) {
                $('#result').html(data);
            }
        })        


    })
</script>

Here is solution that works on page load: 这是适用于页面加载的解决方案:

 <script language="javascript">
$(document).ready(function() {

  $.ajax({
    type: 'POST',
    url: $('#form').attr('action'),
    data: $('#form').serialize(),

    success: function(data) {
      $('#result').html(data);
    }
  })        

 })

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

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