Jquery HTML锚定标记在e.preventDefault()之后不起作用?
当我添加Jquery HTML锚定标记在e.preventDefault()之后不起作用?,jquery,html,Jquery,Html,当我添加e.preventDefault()时在jQuery post方法中,动态生成的锚标记不起作用。 这是jquery代码: 此HTML代码: 虽然user2.js与上述jquery方法相同,但请让您的问题更加具体,即您想要实现什么?如果使用了e.preventDefault(),则不会触发事件的默认操作,即,在您的情况下锚标记将无法工作 EDIT1:如果您想在另一页上使用post方法发送一些数据,然后只使用锚定标记,则可以使用以下代码: 我做了一些修改,如: <a class="
e.preventDefault()时代码>在jQuery post方法中,动态生成的锚标记不起作用。
这是jquery代码:
此HTML代码:
虽然user2.js与上述jquery方法相同,但请让您的问题更加具体,即您想要实现什么?如果使用了e.preventDefault(),则不会触发事件的默认操作,即,在您的情况下锚标记将无法工作
EDIT1:如果您想在另一页上使用post方法发送一些数据,然后只使用锚定标记,则可以使用以下代码:
我做了一些修改,如:
<a class="profile" href="<?php echo "viewProfile.php?id=" . $record['user_id'];?>" >
<img class="img-rounded"
src=" <?php echo "../shadi/images/" . $record['user_photo1'] ?>"
id="<?php echo $record['user_id']; ?>"alt="" width="70%" height="20%">
</a>
EDIT2:我不明白你的问题,但是如果你想检查post请求的响应,你可以使用下面的代码
$(document).ready(function () {
$('.profile').on('click', function (e) {
//Get the href Link
var href = $(this).attr('href');
e.preventDefault();
$.post("LikeMail.php",
{fname: $(this).find(".img-rounded").attr("id")}, function (returnedData) {
// Do whatever you want with returend data
console.log(returnedData);
}).done(function () {
//Redirecting to anchor destination.
//window.location.href = href;
});
});
});
@Zain Farooq请让你的问题更具体,即你想要实现什么?如果使用e.preventDefault(),则不会触发事件的默认操作,例如,在您的情况下,锚定标记将不起作用
EDIT1:如果您想在另一页上使用post方法发送一些数据,然后只使用锚定标记,则可以使用以下代码:
我做了一些修改,如:
<a class="profile" href="<?php echo "viewProfile.php?id=" . $record['user_id'];?>" >
<img class="img-rounded"
src=" <?php echo "../shadi/images/" . $record['user_photo1'] ?>"
id="<?php echo $record['user_id']; ?>"alt="" width="70%" height="20%">
</a>
EDIT2:我不明白你的问题,但是如果你想检查post请求的响应,你可以使用下面的代码
$(document).ready(function () {
$('.profile').on('click', function (e) {
//Get the href Link
var href = $(this).attr('href');
e.preventDefault();
$.post("LikeMail.php",
{fname: $(this).find(".img-rounded").attr("id")}, function (returnedData) {
// Do whatever you want with returend data
console.log(returnedData);
}).done(function () {
//Redirecting to anchor destination.
//window.location.href = href;
});
});
});
e.preventDefault()
旨在防止默认操作发生。在这种情况下,它不会将您发送到您所引用的页面,因为该操作将被取消
您不能让它同时发送POST请求和传送您,因为在页面开始加载新页面之前,POST不会发送
相反,你可以发送帖子,然后更改页面。您只需添加
window.location = this.href;
完成$.post()
后。这会将位置(您所在的页面)更改为a标记中指定的位置。简单:)
由于此
将引用.done()
的承诺,因此您需要返回到单击函数的此
。只需插入一个$this
,并将其设置为单击功能的this
$(document).ready(function() {
$('.img-rounded').on('click', function(e) {
$this = this;
e.preventDefault();
$.post("LikeMail.php", {
fname: this.id
}).done(function() {
window.location = $this.parentNode.href;
});
});
});
编辑:感谢Benjy注意到这一点。您需要执行$this.parentNode.href
,因为这是一个图像。您希望获取父元素,即锚定标记。e.preventDefault()
用于防止发生默认操作。在这种情况下,它不会将您发送到您所引用的页面,因为该操作将被取消
您不能让它同时发送POST请求和传送您,因为在页面开始加载新页面之前,POST不会发送
相反,你可以发送帖子,然后更改页面。您只需添加
window.location = this.href;
完成$.post()
后。这会将位置(您所在的页面)更改为a标记中指定的位置。简单:)
由于此
将引用.done()
的承诺,因此您需要返回到单击函数的此
。只需插入一个$this
,并将其设置为单击功能的this
$(document).ready(function() {
$('.img-rounded').on('click', function(e) {
$this = this;
e.preventDefault();
$.post("LikeMail.php", {
fname: this.id
}).done(function() {
window.location = $this.parentNode.href;
});
});
});
编辑:感谢Benjy注意到这一点。您需要执行$this.parentNode.href
,因为这是一个图像。您希望抓取父元素,即锚点标记。e.preventDefault()
目的正是您所经历的,它阻止锚点执行其默认行为,即跳转到由href
指定的位置。当你说“工作”时,作为一个锚或任何你的功能被期望去做的事情,你是什么意思…因为你没有具体的…工作就像让它跳吉格舞一样?做你的税务?e.preventDefault()
目的正是你所经历的,它阻止锚做它的默认行为,即跳转到href
指定的位置。当你说“工作”时,作为一个锚或任何你的功能被期望去做的事情,你是什么意思…因为你没有具体的…工作就像让它跳吉格舞一样?我在jquery代码中有post方法。为了让它将jquery post数据发送到另一个页面,我必须添加e.preventDefault()
,因为内容在锚定标记中。但是在添加e.preventDefault()
jquery之后开始发送post数据,但锚定标记不起作用。我想修复锚定标记和post methodAnchor标记,但是jquery没有发送数据,我不知道为什么它仍然不工作。我还在其他函数中使用了ajax调用jquery post数据发送到的同一页面(likeemail.php
)。这可能是问题所在吗?我正在将jquery post数据发送到LikeMail.php
页面,并在从viewProfile.php
页面调用ajax后发送到LikeMail.php
。这是一个完整的场景。ajax调用在这里产生问题了吗?@ZainFarooqreq.open('GET','likeemail.php','true')
使用GET方法发布数据,而likeemail.php
使用post方法处理AJAX请求。您必须使用$\u REQUEST
方法来处理这两种类型的请求。另外,这个错误与我的代码无关。我在jquery代码中有post方法。为了让它将jquery post数据发送到另一个页面,我必须添加e.preventDefault()
,因为内容在锚定标记中。但是在添加e.preventDefault()
jquery之后开始发送post数据,但锚定标记不起作用。我想修复锚定标记和post methodAnchor标记,但是jquery没有发送数据,我不知道为什么它仍然不工作。我还在其他函数中使用了ajax调用jquery post数据发送到的同一页面(likeemail.php
)。这可能是问题所在吗?我正在将jquery post数据发送到LikeMail.php
页面,并在从viewProfile.php
页面调用ajax后发送到LikeMail.php
。这是一个完整的场景。ajax叫creatin吗
$(document).ready(function() {
$('.img-rounded').on('click', function(e) {
$this = this;
e.preventDefault();
$.post("LikeMail.php", {
fname: this.id
}).done(function() {
window.location = $this.parentNode.href;
});
});
});