gpt4 book ai didi

php - 使用超链接而不是提交按钮提交 AJAX 表单时出现问题

转载 作者:行者123 更新时间:2023-11-28 15:56:24 25 4
gpt4 key购买 nike

我在使用 JavaScript 和 AJAX 从超链接 (href) 而不是普通输入类型提交 HTML 表单时遇到问题。我当前的代码如下:

Javascript(在 body 标记之前执行):

$(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。

任何帮助将不胜感激。谢谢。

最佳答案

您可以在主脚本中引用链接并捕获点击事件,而不是使用内联 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');
}
});
});
});

关于php - 使用超链接而不是提交按钮提交 AJAX 表单时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18637221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com