我无法在 Ajax 中成功使用大多数 jQuery。我试图让响应中的错误消息出现在我的通知(toast)div中,但是除了 .show() 和 .hide() 之外我尝试的任何内容都不会工作。
我已经使用console.log()来确保它确实解码了来自url的响应,并且一切都按预期进行,但是我无法调用Ajax成功函数内的函数 p>
这是我当前的 JS,点击按钮即可触发。
function errMsg(code, msg) {
const eCode = '<b>E-NS: ' + code + ' </b> </br>' + msg;
const eMsg = '<span class="err" style="color: red;">' + eCode + '</span>';
$('.notify').empty().html(eMsg);
}
$(document).ready(function() {
$('#next-1').click(function(e) {
e.preventDefault();
$.ajax({
url:'../data.php',
method: 'POST',
data: $('#form-1').serializeArray(),
dataType: 'JSON',
success:function(response){
if(response.status === true){
$('#form-1').hide();
$('#form-2').show();
} else {
console.log(response.status);
console.log('Response = ' + response.code + response.error);
errMsg(response.code, response.error);
var eCode = '<b>E-NS: ' + response.code + ' </b> </br>' + response.error;
var eMsg = '<span class="err>' + eCode + '</span>';
$('.notify').empty().html(eMsg);
}
}
})
})
)}
需要注意的是,php 或 jQuery 中没有出现错误或警告。我是 AJAX 新手,所以我不完全确定这是否是我能够做的事情,尽管它看起来足够合乎逻辑,不是吗?
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
我隐藏了我的 toast div (
$('.notify')),直到 div 中有文本。但是,我忽略了添加显示此内容的函数(如下所示),并且在使用errorCheck() 更新 AJAXsuccess函数中的 toast 后没有调用它代码>function errorCheck() { if ($('.notify').text().trim().length == 0) { $('.notify').hide(); } else { var time = setTimeout(function() { $('.notify').fadeOut('200'); }, 5000); $('.notify') .show() .mouseout(function () { time; }) .mouseover(function () { clearTimeout(time); }) .click(function() { $('.notify').fadeOut('100'); }); } }