我在使用JavaScript&提交HTML表单时遇到问题。 AJAX来自超链接(href)而不是普通的输入类型。我目前的代码如下:
Javascript(在正文标记之前执行):
$(document).ready(function () {
//Chronicle Form 1
$('#fm_chronicledate').submit(function(event) {
var data = $('#fm_chronicledate').serialize() + 'cron=1' + '&chron_date=' + chron_date.val() + '&chron_time=' + chron_time.val();
$.ajax({
type: "GET",
url: "script.php",
data: data,
cache: false,
success: function(html) {
if(html == 1){
$('#chron_part1').fadeOut('slow');
} else alert('Error');
}
});
return false;
});
});
HTML:
<form id="fm_chronicledate" method="POST" action="#" >
<input type="text" class="website" name="chron_time" id="chron_time" />
<input type="text" class="website" name="chron_date" id="chron_date" />
<a href="#" class="chron_submit1" id="chron_submit1" onclick="fm_chronicledate.submit(); return false;">Submit</a>
</form>
无论如何由于某种原因它只是不执行AJAX。
非常感谢任何帮助。 谢谢。
答案 0 :(得分:3)
您可以在主脚本中引用链接并捕获click事件,而不是使用内联javascript,就像这样......
<强> HTML:强>
<form id="fm_chronicledate" method="POST" action="#" >
<input type="text" class="website" name="chron_time" id="chron_time" />
<input type="text" class="website" name="chron_date" id="chron_date" />
<a href="#" class="chron_submit1" id="chron_submit1">Submit</a>
</form>
<强>的Javascript 强>
$(document).ready(function () {
$('#chron_submit1').on("click", function(event) {
var data = $('#fm_chronicledate').serialize() +
'cron=1' +
'&chron_date=' + $("#chron_date").val() +
'&chron_time=' + $("#chron_time").val();
$.ajax({
type: "GET",
url: "script.php",
data: data,
cache: false,
success: function(html) {
if (html == 1) {
$('#chron_part1').fadeOut('slow');
}
else
alert('Error');
}
});
});
});
答案 1 :(得分:1)
你为什么不这样做:
<a href="#" class="chron_submit1" id="chron_submit1" onclick="Javascript:GetAjax()">Submit</a>
$(document).ready(function () {
function GetAjax() {
var data = $('#fm_chronicledate').serialize() + 'cron=1' + '&chron_date=' + chron_date.val() + '&chron_time=' + chron_time.val();
$.ajax({
type: "GET",
url: "script.php",
data: data,
cache: false,
success: function(html) {
if(html == 1){
$('#chron_part1').fadeOut('slow');
} else alert('Error');
}
});
return false;
}
});
答案 2 :(得分:0)
首先从链接中删除onclick并将其更改为
<form id="fm_chronicledate" method="POST" action="#" >
<input type="text" class="website" name="chron_time" id="chron_time" />
<input type="text" class="website" name="chron_date" id="chron_date" />
<a href="javascript:void(0)" class="chron_submit1" id="chron_submit1">Submit</a>
</form>
将您的活动更改为
$('#chron_submit1')。click(function(event){
在序列化表单时也会出错。必须是
var data = $('#fm_chronicledate')。serialize()+'cron = 1'+ '&amp; chron_date ='+ $(chron_date).val()+'&amp; chron_time ='+ $(chron_time).VAL();