我正在开发一个网页,动态创建一个包含各种选择标签的表单。 unfortenatley jquery提交函数将不正确的信息传递给必须处理此信息的php文件(form_submit.php); (netbeans 7.4)调试器总是在php文件中的$ _POST中显示默认(未选中)值而不是所选值。更奇怪的部分:当我将带有序列化formdata的控制台输出直接复制到下面的ajaxcode(参见代码)时,php调试器在$ _POST数组中显示正确的选择值..(?)
结果形式相当可观。我能想到的唯一理论原因是因此执行'var dataAjax = $(this).serialize();'当下面的ajax调用开始时,需要时间并且没有完成...(??)
代码:
$("#myForm").submit(function(event){
event.preventDefault();
var dataAjax = $( this ).serialize(); //does not work, but when copying the string from the console in line below it does work.
console.log('SUBMITTED FORM: '+ dataAjax );
//next line is only used as a test
//var dataAjax = 'agreementapproval0=SolarX_10&selected_emeterproductid0=SolarX_10_1_1&selected_eproductid0=SolarX_10_2_4&selected_emeterproductid1=NOSELECTION&selected_emeterproductid2=NOSELECTION&selected_eproductid1=NOSELECTION&selected_eproductid2=NOSELECTION&form_token=30688e467ee88167805ad0f330809d74';
$.ajax({
type: "POST",
url: "form_submit.php",
data: dataAjax,
dataType: "json",
async: true,
success: function(msg){
if(msg.statusgeneral == 'success'){
}
else
{
}//else
}, //succes: function
error: function(){
$("#errorbox").html("There was an error submitting the form. Please try again.");
}
});//.ajax
//make sure the form doesn't post
//return false; //depreciated
//} //if(form.valid()
});//$("#myForm").submit()
<form id="myForm" name="myForm" action="" method="post">
<div id="wrapper"></div> <!--anchor point for adding set of product form fields -->
<input type="hidden" name="form_token" value="<?php echo $form_token; ?>" />
<input type="submit" name="submitForm" value="Confirm">
</form>
答案 0 :(得分:1)
工作正常http://jsfiddle.net/a9DN4/
您可能在创建之前尝试挂钩表单,尝试使用
包装代码$( document ).ready(function() {
// Handler for .ready() called.
});
PS并将event.preventDefault();
移动到你的函数的第一个字符串,正如凯文所说。