2017-08-27 16 views
1

我有一些動態創建的內容,其中包括一個小表單(下拉按鈕)分配給某人的任務。序列化動態創建的表單//發佈阿賈克斯請求

使用jquery,我想用新分配的人來更新我的數據庫。我的方法:

$('#messageDetail').on('click', '#assignButton', function() { 
    $.ajax({ 
     url: "admin_assginMessage.php", 
     type: "post", 
     data: $("#assignForm").serialize(), 
     success: function (data) { 
      alert("Success!"); 
     }, 
     error: function() { 
      alert("Error!"); 
     } 
    }); 
}); 

看來它不會發送給我的表單輸入。我有我需要使用。對感覺()在這裏,但我不知道我應該選擇哪一個事件

data: $("#assignForm").serialize(), 

感謝所有的答案!

+0

你試過使用** FormData()**變量嗎?通過執行以下錯誤也可以看到錯誤:function(error){console.log(error);}'' – vibs2006

回答

0

您應該使用event.preventDefault()這裏更多:event.preventDefault()

<script> 
$("#assignForm").submit(function(e) { 
    e.preventDefault(); 

    $.ajax({ 
     url: "admin_assginMessage.php", 
     type: "post", 
     data: $("#assignForm").serialize(), 
     success: function (data) { 
      alert("Success!"); 
     }, 
     error: function() { 
      alert("Error!"); 
     } 
    }); 
}); 
</script> 
0
Try assigning post data to a variable 

` 
$('#messageDetail').on('click', '#assignButton', function() { 
    $.ajax({ 
     url: "admin_assginMessage.php", 
     type: "post", 
     data: {'mydata' : $("#assignForm").serialize()}, 
     success: function (data) { 
      alert("Success!"); 
     }, 
     error: function() { 
      alert("Error!"); 
     } 
    }); 
}); 
` 
0

如果你說「我創建了一個形式動態,但是當我試圖序列化,我什麼都看不到有關動態表單的數據「解決的是在這裏:

const dynamicForm = ` 
 
\t <form id="frmData" onsubmit="return false"> 
 
\t <label>Username 
 
\t <input type='text' name='username' /> 
 
\t </label> 
 

 
\t <label>Password 
 
\t <input type='password' name='password' /> 
 
\t </label> 
 
\t <button id="clickSend">Click and Send</button> 
 
\t </form> 
 
`; 
 

 
$("#addForm").on("click",() => { 
 
\t $("#frmarea").append(dynamicForm); 
 
}) 
 

 
/* Send Form Details with Dynamic Form */ 
 

 
$(document).on("click", "#clickSend",() => { 
 
\t let serializeData = $("#frmData").serialize() 
 
\t 
 
\t console.log(serializeData) 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="frmarea"></div> 
 

 
<button id="addForm">Add Form</button>

因此,當您使用動態生成的表單時,應該使用$(document)