我试图使用下面的代码提交带有ajax的表单,但显然似乎无法正常工作。
有任何解决此问题的建议吗?
$(".dialog-set-new-message").on('keyup', '.ctxtareados', function(b){
if(b.keyCode == 8){return false;}
if(b.keyCode == 13){
//
$("#submit_new_message_cbox").submit(function(eventmsg){
$(".loader-wait-gif-con").fadeIn(500);
eventmsg.preventDefault();
$.ajax({
url: "https://www.mypage.com/success/set_new_cbox_message.php", // Url to which the request is send
type: "POST", // Type of request to be send, called as method
data: new FormData(this), // Data sent to server, a set of key/value pairs representing form fields and values
contentType: false, // The content type used when sending data to the server. Default is: "application/x-www-form-urlencoded"
cache: false, // To unable request pages to be cached
processData:false, // To send DOMDocument or non processed data file it is set to false (i.e. data should not be in the form of string)
success: function(data) // A function to be called if request succeeds
{
$(".dialog-new-chat-open-con").load("../success/refresh_new_cbox.php", function(){
$(".loader-wait-gif-con").fadeOut(500);
$(".con-sub-nav-chat h3").html("Messages updated.");
$(".con-sub-nav-chat").css("display", "block");
$(".con-sub-nav-chat").fadeOut(7000);
$(".all-msg-new-chat-box").animate({scrollTop: $("#sldum").offset().top}, 1000);
});
$('#submit_new_message_cbox')[0].reset();
}
});
});
//
}
});
- 修正 -
我在php文件中添加了一个输入[submit]并修改了代码如下:
PHP代码添加:
<input style="visibility: hidden;" id="submitnmcbox" type="submit">
JS Code Modified(与上面相同):
$(".dialog-set-new-message").on('keyup', '.ctxtareados', function(b){
if(b.keyCode == 8){return false;}
if(b.keyCode == 13){
$("#submitnmcbox").click();
}
});
//Set new msg chat
$("#submit_new_message_cbox").on('submit', function(eventmsg){
$(".loader-wait-gif-con").fadeIn(500);
eventmsg.preventDefault();
$.ajax({
url: "https://www.dudoby.com/success/set_new_cbox_message.php", // Url to which the request is send
type: "POST", // Type of request to be send, called as method
data: new FormData(this), // Data sent to server, a set of key/value pairs representing form fields and values
contentType: false, // The content type used when sending data to the server. Default is: "application/x-www-form-urlencoded"
cache: false, // To unable request pages to be cached
processData:false, // To send DOMDocument or non processed data file it is set to false (i.e. data should not be in the form of string)
success: function(data) // A function to be called if request succeeds
{
$(".dialog-new-chat-open-con").load("../success/refresh_new_cbox.php", function(){
$(".loader-wait-gif-con").fadeOut(500);
$(".con-sub-nav-chat h3").html("Se han actualizado los mensajes.");
$(".con-sub-nav-chat").css("display", "block");
$(".con-sub-nav-chat").fadeOut(7000);
$(".all-msg-new-chat-box").animate({scrollTop: $("#sldum").offset().top}, 1000);
});
$('#submit_new_message_cbox')[0].reset();
}
});
});
//
答案 0 :(得分:0)
我认为您错误地使用了FormData()
构造函数。根据{{3}},FormData()
构造函数接受HTML DOM表单元素(可选)。例如:
<form name="myForm">
<input type="text" name="text_field_1" value="test1" /><br /><br />
<input type="text" name="text_field_2" value="test2" /><br /><br />
<input type="text" name="text_field_3" value="test3" /><br />
</form>
<script type="text/javascript">
var myForm = document["myForm"];
var formData = new FormData(myForm);
console.log(formData.get("text_field_2")); // returns "test2"
</script>