簡體   English   中英

在頁面加載時提交帶有ajax的表單

[英]Submit a form with ajax on page load

我有此表單,希望在頁面加載時提交。

我的jQuery代碼當前如下:

<script> 
    // wait for the DOM to be loaded 
    $(document).ready(function() { 
      document.getElementById('form1').submit();
    }); 
</script> 

我嘗試過ajax表單插件( http://malsup.com/jquery/form/ ),該插件確實提交了表單,但是我不知道如何在頁面加載時使用它:

  <script> 
    // wait for the DOM to be loaded 
    $(document).ready(function() { 
        $('#form1').ajaxForm(function() { 
            alert("Thank You"); 
        }); 
    }); 
</script> 

救命!

我只想在頁面加載后提交form1,該插件看起來是我最好的選擇,但是我不知道如何在頁面加載時使用它

嘗試這個

$(document).ready(function() { 
    $('#btnSubmit').click(); //assuming this is your submit button
}); 

希望對您有幫助。

我希望這會做你想要的:)

jQuery(document).ready(function(){

  // #1 get the data to submit
  var jInput;
  var oData  = {};
  var aInput = jQuery('#form1 input');
  var i      = aInput.length;
  while ( --i !== -1 ) {
    jInput = jQuery( aInput[i] );
    oData[ jInput.attr('id') ] = jInput.val();
    console.log( jInput.val() );
  }

  // #2 submit data via ajax (method == 'post')
  // without response
  jQuery.post( 'url-to-send-ajax-request-to.php', oData );

  // with response
  jQuery.post( 'url-to-send-ajax-request-to.php', oData, function( sResponse ){
    alert( sResponse ); // would be the output of the file you sent your ajax request to
    alert('Thank You');
  });

});

PHP文件(“發送到ajax請求到.php的網址”):

print_r( $_POST );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM