Javascript 如何使用jQuery删除元素?
我在php while循环中有一些按钮,这些按钮有一个onClick方法。我想删除我选择的按钮 这是我在按钮上的代码:Javascript 如何使用jQuery删除元素?,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我在php while循环中有一些按钮,这些按钮有一个onClick方法。我想删除我选择的按钮 这是我在按钮上的代码: while($row = $query->fetch_assoc()) { $Var1= $row ['Id']; $Description= $row ['Description']; $Detail= $row ['Detail']; ?> <button class="btn btn-dan
while($row = $query->fetch_assoc())
{
$Var1= $row ['Id'];
$Description= $row ['Description'];
$Detail= $row ['Detail'];
?>
<button class="btn btn-danger btn-block" onClick="Hello('<?php echo $Var1; ?>','Delete')">
<div class="pull-left first">
<i class="fa fa-bell" aria-hidden="true"></i> <?php echo ucwords($Description); ?>
</div>
<div class="pull-left notification">
<?php echo ucfirst($Detail); ?>
</div>
</button>
}
while($row=$query->fetch\u assoc())
{
$Var1=$row['Id'];
$Description=$row['Description'];
$Detail=$row['Detail'];
?>
将事件函数与html代码分开。这是一个很好的做法。
首先,您的ID应该是唯一的,所以为您的按钮创建一个类
$('.some class')。在('click',函数(事件){
event.preventDefault();
$(this.remove();
})
按钮将事件函数与html代码分开。这是一个很好的做法。
首先,您的ID应该是唯一的,所以为您的按钮创建一个类
$('.some class')。在('click',函数(事件){
event.preventDefault();
$(this.remove();
})
ES6之前的按钮
当时,自杀是不允许的,只有父母才能杀死他们的孩子
this.parentNode.removeChild(this)
ES6及以上
this.remove()
JQuery
$( selector ).remove()
ES6之前
当时,自杀是不允许的,只有父母才能杀死他们的孩子
this.parentNode.removeChild(this)
ES6及以上
this.remove()
JQuery
$( selector ).remove()
首先,正如Aibol Orazbekov所建议的,您应该使用类名来启动特定的函数
我的建议是:
- 不要使用
onClick
参数
- 您应该使用dataset将变量传递给Javascript函数
- 您应该使用HTTP代码来处理错误(并使用jQuery的ajax
error()
函数来处理错误)
- 不要对简单变量名称使用大写字母。仅对常量使用大写字母
为了回答您的问题,$(此)
未链接到您的元素
根据您的代码,并使用类名,您可以尝试以下操作:
HTML/PHP
while($row = $query->fetch_assoc())
{
$var1= $row ['Id'];
$description= $row ['Description'];
$detail= $row ['Detail'];
?>
<button class="btn btn-danger btn-block btn-action" data-id="<?php echo $var1; ?>" data-action="Delete">
<div class="pull-left first">
<i class="fa fa-bell" aria-hidden="true"></i> <?php echo ucwords($description); ?>
</div>
<div class="pull-left notification">
<?php echo ucfirst($detail); ?>
</div>
</button>
}
while($row=$query->fetch\u assoc())
{
$var1=$row['Id'];
$description=$row['description'];
$detail=$row['detail'];
?>
首先,正如Aibol Orazbekov所建议的,您应该使用类名来启动特定的函数
我的建议是:
- 不要使用
onClick
参数
- 您应该使用dataset将变量传递给Javascript函数
- 您应该使用HTTP代码来处理错误(并使用jQuery的ajax
error()
函数来处理错误)
- 不要对简单变量名称使用大写字母。仅对常量使用大写字母
为了回答您的问题,$(此)
未链接到您的元素
根据您的代码,并使用类名,您可以尝试以下操作:
HTML/PHP
while($row = $query->fetch_assoc())
{
$var1= $row ['Id'];
$description= $row ['Description'];
$detail= $row ['Detail'];
?>
<button class="btn btn-danger btn-block btn-action" data-id="<?php echo $var1; ?>" data-action="Delete">
<div class="pull-left first">
<i class="fa fa-bell" aria-hidden="true"></i> <?php echo ucwords($description); ?>
</div>
<div class="pull-left notification">
<?php echo ucfirst($detail); ?>
</div>
</button>
}
while($row=$query->fetch\u assoc())
{
$var1=$row['Id'];
$description=$row['description'];
$detail=$row['detail'];
?>
你能发布你按钮的所有代码吗?(你把jquery代码放在哪里删除它?@Taki我更新我的问题谢谢你的时间:在这种情况下,我认为$(this).remove()基于$。AJAX不在按钮中,尝试移动$(this).remove()
到另一个地方,比如添加一个全局变量var issucces=false
,然后在案例“3”中:/*code*/issucces=true;
因此,在$.ajax之后,您可以测试变量“issucces”是否为true,然后,只需使用$(this.remove()
)删除即可。检查是否可以发布按钮的所有代码吗?(你把jquery代码放在哪里删除它?@Taki我更新我的问题谢谢你的时间:在这种情况下,我认为$(this).remove()基于$。AJAX不在按钮中,请尝试移动$(this).remove()
到另一个地方,比如添加一个全局变量var issucces=false
,然后在案例“3”中:/*code*/issucces=true;
因此,在$.ajax之后,您可以测试变量“issucces”是否为true,然后,只需使用$(此).remove()
。检查是否有效抱歉我更新我的问题谢谢你的时间:d抱歉我更新我的问题谢谢你的时间:DHello@Mike!!抱歉,我更新我的问题谢谢你的时间:d此代码仍然有效,可以删除你的元素OnClick事件。如果你在jQuery的回调中使用它,那么“this”引用更改。在这种情况下,你可以在ajax调用之前,应该将其保存在其他变量中,如:var that=this;然后使用'that'代替我的jQuery或ajax真的很基本:x,我尝试了你说的,但抛出了这个错误**未捕获类型错误:无法读取此属性,删除未定义的**,我使用了你给我的代码:函数hello(ID,action){var that=this;this.parentNode.removeChild(that);}
Hello@Mike!!很抱歉,我更新了我的问题谢谢您的时间:数据此代码仍然有效,可以删除元素OnClick事件。如果您在jQuery的回调中使用它,则“this”引用更改。在这种情况下,您应该在ajax调用之前将其保存在其他变量中,如:var that=this;然后使用“that”代替我的jQuery或ajaxbasic:x,我尝试你说的,但抛出了这个错误**未捕获类型错误:无法读取这个属性removeChild of undefined**,我使用了你给我的代码:function hello(ID,action){var that=this;this.parentNode.removeChild(that);}
非常感谢您,我必须研究您的所有评论,我不知道为什么不使用var elt=$(this)elt.remove();
,但现在我解决了我的问题,将id添加到与行的$id连接的按钮中,id=“Delete”
,并在函数上添加$('#Delete'+id)…它现在可以使用,但我希望有更好的实践,因此感谢您的所有评论:D@micster非常感谢,我必须研究您的所有评论,我不知道为什么不使用var elt=$(this)elt.remove();
,但现在我解决了我的问题,将id添加到与行的$id连接的按钮中,id=“删除”
和o