簡體   English   中英

如何提交帶有<a href>鏈接</a>的AJAX表單<a href>?</a>

[英]How to submit an AJAX form with a <a href> link?

我有以下表格:

<form id='confirm_reset' action='login/forgotPassword_action' method='post'>
<input type='hidden' name='user_email' value='user_email'>
<a href='#' onclick="confirm_reset.submit();">Submit</a>
</form>

<div id="alert_box_register"></div>

我正在嘗試使用Ajax提交此文件,以在警報框中返回JSON:

$("#confirm_reset").on("submit", function(event) {

   //disable default click operation
   event.preventDefault();

   var action_url = $(this).attr("action");

   alert_box_register("Resetting password...");

   console.log(action_url);

   var postData = $(this).serializeArray();
   console.log(postData);

   $.post(action_url, postData, function(data) { 
      console.log(data);
      var obj = $.parseJSON(data); 

      alert_box_register(obj.message); 
   });

});

該腳本不返回任何結果(就像鏈接不起作用一樣)。 我要去哪里錯了?

不知道該代碼是否仍然對您有問題...?

關於進度消息的快速注釋("Resetting password...") :此代碼可能會運行得如此之快,以至於該消息幾乎不會在用戶的屏幕上閃爍。 我不知道您的資料如何設置,但您可能永遠不會在屏幕上看到它。

<!-- The following line was missing .php from the action -->
<!--form id='confirm_reset' action='login/forgotPassword_action' method='post'-->
<form id='confirm_reset' action='login/forgotPassword_action.php' method='post'>
   <input name="txtbox" type="text" value="hello world"/>
   <input type='hidden' name='user_email' value='user_email'>

   <!-- submit_confirm_reset() is a function I made in the javascript tages-->
   <a href='#' onclick="submit_confirm_reset();">Submit</a>
</form>

<div id="alert_box_register"></div> 

<script>

function submit_confirm_reset() {
   $("#confirm_reset").submit(); 
}

$("#confirm_reset").on("submit", function(event) 
{ 
   console.log('("#confirm_reset").on("submit", function(event)');

   //disable default click operation
   event.preventDefault();

   var action_url = $(this).attr("action"); 

   // you were using alert_box_register like it was a function
   // but it doesn't exist in the code you posted in your question
   // but a DOM element has this name so I assume you meant that
   $("#alert_box_register").html("Resetting password..."); 
   console.log(action_url);

   var postData = $(this).serializeArray();
   console.log(postData);

   $.post(action_url, postData, function(data) { 
      console.log(data);

      // if this response is already json, you don't need to parse it 
      var obj = $.parseJSON(data);  
      $("#alert_box_register").html(obj.message); 
   }); 
});
</script>

暫無
暫無

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

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