<head runat="server">
<script type="text/javascript">
$.fn.serializeObject = function () {
var o = {};
var a = this.serializeArray();
$.each(a, function () {
if (o[this.name] !== undefined) {
if (!o[this.name].push) {
o[this.name] = [o[this.name]];
}
o[this.name].push(this.value || '');
} else {
o[this.name] = this.value || '';
}
});
return o;
};
// we will add our javascript code here
$(document).ready(function () {
$("#myForm").submit(function () {
var str = $("#myForm").serialize();
var str = JSON.stringify($('#myForm').serializeObject());
alert(str);
$.ajax({
type: 'POST',
contentType: 'application/json; charset=utf-16',
data: str,,
dataType: 'json',
url: "testAjaxPost.aspx/SaveCustomerEmail",
success: function (msg) {
alert('done');
return true;
},
error: function (msg) { alert(JSON.stringify(msg)) }
});
return form.submit();
return false;
});
});
我希望将表单数据发布到两个页面,为此我使用表单操作方法和jQuery表单提交方法。但只有jQuery表单提交事件有效,但表单方法发布事件不起作用。我怎么能这样做?
function ValidateForm() {
var emailID = $(".email").val();
if (!validate_email_address(emailID)) {
alert("Please provide a valid email address.");
$(".email").focus();
return false;
}
}
function validate_email_address(email) {
var regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
return regex.test(email);
}
</script>
</head>
<body>
<form id="myForm" action="page1.aspx" method="post" onsubmit="return ValidateForm()">
<label>
Email*</label>
<input type="text" name="Email" class="email" />
<input type="submit" title="Save" />
</form>
</body>
答案 0 :(得分:0)
function ValidateForm() {
//Data Post to FirstForm
var str = $("#FirstForm").serialize();
$.ajax({
type: "POST",
dataType: "jsonp",
contentType: "application/json; charset=utf-8",
url: "https://xyz.com",
data: str,
success: function (msg) {console.log("Success data post ---->" + JSON.stringify(msg)) },
error: function (msg) { console.log("Error data post ---->" + JSON.stringify(msg)) }
});
//End Data Post to First Form
//Call Click event of Second form .Use Form post and action method .
//Copy First Form text box value into second Form text box
$("#btnSecondForm").trigger('click');
//End Click event
return false;
}
//Form in Body
<form id="FirstForm" onsubmit="return ValidateForm(this)">
//text box HTML
</form>
<form id="SecondForm" style='display:none' action="https://abc.com" method="post">
<input type="submit" value="Submit" id='btnSecondForm' />
</form>