JS / AJAX:使用计时器提交表单,而不是单击按钮或刷新页面

时间:2022-11-24 15:28:11

I am trying to submit a form without page refresh or a submit button. But i have only achieved to have the JS function submit the input box value. Is it possible to submit the whole form without a button click and refresh of page?

我正在尝试提交没有页面刷新或提交按钮的表单。但我只是实现了JS函数提交输入框值。是否可以在没有按钮点击和刷新页面的情况下提交整个表单?

JSFIDDLE

JS

            <script type="text/javascript">
                $(document).ready(function() {
                    var timer;
                        $('#yurl).on('keyup', function() {
                            var value = this.value;

                            clearTimeout(timer);

                            timer = setTimeout(function() {

                                //do your submit here

                                alert('submitted:' + value);
                            }, 2000);
                        });
                });
            </script>

html

                <form method="post" id="ytVideo" action="">
                <input id="yurl" type="text" value="<?php $url ?>" name="yurl">     
                </form>

3 个解决方案

#1


1  

Your fiddle seems to be working but the problem is that you're not really submitting it anywhere.

你的小提琴似乎工作,但问题是你并没有真正提交任何地方。

<script type="text/javascript">
 $(document).ready(function() {
                        var timer;
                            $('#yurl).on('keyup', function() {
                                var value = this.value;

                                clearTimeout(timer);

                                timer = setTimeout(function() {

                                    //do your submit here
                                    $("#ytVideo").submit()
                                    alert('submitted:' + value);
                                }, 2000);
                            });


     //then include your submit definition. What you want to do once submit is executed
      $('#ytVideo').submit(function(e){
           e.preventDefault(); //prevent page refresh
           var form = $('#ytVideo').serialize();
           //submit.php is the page where you submit your form
           $.post('submit.php', form, function(data){ 
              //do something with the data

           });
      });

});
</script>

#2


1  

I think jQuery.serialize() could solve your problem quite nicely.

我认为jQuery.serialize()可以很好地解决你的问题。

#3


1  

If I understood well what you're asking, where it says //do your submit here you should put:

如果我很清楚你在问什么,它在哪里说//你在这里提交你应该把:

 $("#ytVideo").submit()

where ytVideo is the id of the form you're trying to submit

其中ytVideo是您尝试提交的表单的ID

Good luck!

#1


1  

Your fiddle seems to be working but the problem is that you're not really submitting it anywhere.

你的小提琴似乎工作,但问题是你并没有真正提交任何地方。

<script type="text/javascript">
 $(document).ready(function() {
                        var timer;
                            $('#yurl).on('keyup', function() {
                                var value = this.value;

                                clearTimeout(timer);

                                timer = setTimeout(function() {

                                    //do your submit here
                                    $("#ytVideo").submit()
                                    alert('submitted:' + value);
                                }, 2000);
                            });


     //then include your submit definition. What you want to do once submit is executed
      $('#ytVideo').submit(function(e){
           e.preventDefault(); //prevent page refresh
           var form = $('#ytVideo').serialize();
           //submit.php is the page where you submit your form
           $.post('submit.php', form, function(data){ 
              //do something with the data

           });
      });

});
</script>

#2


1  

I think jQuery.serialize() could solve your problem quite nicely.

我认为jQuery.serialize()可以很好地解决你的问题。

#3


1  

If I understood well what you're asking, where it says //do your submit here you should put:

如果我很清楚你在问什么,它在哪里说//你在这里提交你应该把:

 $("#ytVideo").submit()

where ytVideo is the id of the form you're trying to submit

其中ytVideo是您尝试提交的表单的ID

Good luck!