JS/AJAX: Submit form with timer and not a button click or refresh of page

前端 未结 3 660
迷失自我
迷失自我 2021-01-14 15:03

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 t

相关标签:
3条回答
  • 2021-01-14 15:48

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

    0 讨论(0)
  • 2021-01-14 16:01

    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

    Good luck!

    0 讨论(0)
  • 2021-01-14 16:08

    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>
    
    0 讨论(0)
提交回复
热议问题