为什么警报仅适用于第一行
Why alert works only on first row?
我使用 Zebra 对话框,每次单击"删除"时都会尝试发出警报。仅当我单击第一行中的"删除"时,警报才有效。它下面的所有行都不起作用,我不知道为什么?
<table>
<tr>
<td>Example1</td>
<td><a id="delete" href="#">Delete</a></td>
</tr>
<td>Example1</td>
<td><a id="delete" href="#">Delete</a></td>
</tr>
<td>Example1</td>
<td><a id="delete" href="#">Delete</a></td>
</tr>
</table>
<script>
$(document).ready(function(){
$("#delete").bind("click",function(e){
e.preventDefault();
$.Zebra_Dialog("Do you want to delete?",{
type : "question",
title: "Question"
});
});
});
</script>
Id 必须是唯一的。这在这里造成了问题。因此,为了使您的代码正常工作,通过将其更改为类来进行一些小的更改。
将标记更改为
<table>
<tr>
<td>Example1</td>
<td><a class="delete" href="#">Delete</a></td>
</tr>
<td>Example1</td>
<td><a class="delete" href="#">Delete</a></td>
</tr>
<td>Example1</td>
<td><a class="delete" href="#">Delete</a></td>
</tr></table>
然后在jquery中
<script>
$(document).ready(function(){
$(".delete").bind("click",function(e){ <----- class selector
e.preventDefault();
$.Zebra_Dialog("Do you want to delete?",{
type:"question",
title:"Question"
})
// send an ajax request here based up on the user selection
});
});
</script>
如果您是初学者,请在此处阅读标准指南。
元素
的ID属性必须是文档中的唯一值,在您的情况下,所有删除链接都具有相同的ID。如果有多个元素共享一个共同的行为,请使用公共类属性将它们组合在一起。
<table>
<tr>
<td>Example1</td>
<td><a class="delete" href="#" data-id="1">Delete</a>
</td>
</tr>
<tr>
<td>Example1</td>
<td><a class="delete" href="#" data-id="2">Delete</a>
</td>
</tr>
<tr>
<td>Example1</td>
<td><a class="delete" href="#" data-id="3">Delete</a>
</td>
</tr>
</table>
然后
$(document).ready(function () {
$(".delete").bind("click", function (e) {
e.preventDefault();
var $this = $(this);
$.Zebra_Dialog("Do you want to delete?", {
type: "question",
title: "Question",
buttons: ['Delete', 'Cancel'],
onClose: function (caption) {
if (caption == 'Delete') {
$.ajax({
url: 'delete.php',
data: {
id: $this.data('id')
}
}).done(function(){
$this.closest('tr').remove();
}).fail(function(){
alert('there was an error while deleting the record')
})
//code required to delete the record from server goes in here
}
}
})
});
});
演示:小提琴
相关文章:
- JQueryhide()不适用于Mozzilla,但适用于Chrome
- PHP中的setcookie仅适用于localhost
- html5 drawImage适用于firefox,而不是chrome
- 提供“;onClick"适用于iPad(触摸屏)和桌面用户的默认功能
- jQuery表单验证适用于Mozilla和Internet Explorer,但不适用于Chrome或Safari
- ResolveUrl是否适用于././也
- Firebase updateChildValues适用于IOS,但不适用于Web和Android
- JavaScript警报适用于int,但不适用于string
- SQLite插件适用于Mac和Windows,但不适用于手机上的Safari
- Regex Replace仅适用于Last Match
- window.onload适用于aspx页面,但不适用于普通html
- Jquery Ajax POST不工作.适用于GET
- 画布上的自定义字体仅适用于safari
- Javascript仅适用于alert()和Debug模式
- JS适用于Firefox和Safari,但不适用于Chrome.此处'是我的网站
- Regex不适用于Firefox,但适用于Chrome
- 通过单击主菜单外部关闭子菜单'不适用于IE,但适用于Firefox&铬
- AngularJS$http.post没有'不适用于Chrome,仅适用于IE
- Javascript仅适用于jQuery mobile中的页面刷新
- 关闭当前选项卡并打开一个新的-仅适用于Chrome