我想用插件sweetalert显示警报消息。我有一个页面,在索引页面上显示10个产品每个都有链接(可能是带有隐藏字段的表单),其中包含要发送到php脚本的数据
例如script.php?id=x&b=x&c=z
有HTML与PHP
<?php foreach($datas as $data): ?>
<a href="auction_buy.php?id=<?php echo $data->$id; ?>&b=x&c=z" class="a">link</a>
<?php endforeach; ?>
$data是具有来自DB 的数据的对象
现在,如果我添加ajax
$('.a').click(function (event){
event.preventDefault();
$.ajax({
url: $(this).attr('href')
,success: function(response) {
swal("Here's a message!");
//Similar to alert(''); i tried alert too but it didnt work
}
})
return false;
});
警报("ok")或摇摆警报不会出现。我有一个产品描述页面,其中的代码与表单数据类似那么这可能是多个链接的问题吗?
工作的产品描述中的代码
jQuery("#contactForm1").ready(function() {
var frm = $('#contactForm1');
frm.submit(function (ev) {
$.ajax({
type: frm.attr('method'),
url: frm.attr('action'),
data: frm.serialize(),
success: function (data) {
swal({
title: "Success!",
text: "Code worked",
type: "success",
confirmButtonColor: "#4B77BE",
confirmButtonText: "Ok",
closeOnConfirm: true
},
function() {
location.reload();
}
);
}
});
ev.preventDefault();
});
});
我需要的是在ajax函数之前或之后显示警报。我真的花了很多时间来弄清楚这一点,但我是jquery的初学者,有人能帮我吗?非常感谢您,并为糟糕的英语感到抱歉:)
试试这个:
$('.a').click(function (event){
$.ajax({
url: $(this).attr('href')
})
.done(function(response) {
swal("Here's a message!");
)};
//it should be here after ajax
event.preventDefault();
});
您应该在末尾声明event.prventDefault()。还要记住,成功回调从jQuery 1.8开始就被弃用了,您应该使用done()。