我想通过点击<a>
<a class="delete-button'" href="/Customer/Delete/345">Delete</a>
这个事件。
我怎么做?
$(function () {
function Delete(event) {
myConfirm('Do you want to delete this record ?',
function () {
alert('You clicked OK'); // It should be 345 here
},
function () {
alert('You clicked Cancel');
},
'Confirm Delete');
return false;
}
$('a.delete-button').click(Delete);
});
谢谢!
答案 0 :(得分:3)
我会通过在链接标记的数据属性中添加“345”来实现:
<a class="delete-button" data-customer-id="345" href="/Customer/Delete/345">Delete</a>
然后你的删除功能可以使用.data()
方法引用它,如下所示:
function Delete(event) {
var self = $(this);
myConfirm('Do you want to delete this record ?',
function () {
alert('You clicked ' + self.data("customer-id")); // It should be 345 here
},
function () {
alert('You clicked Cancel');
},
'Confirm Delete');
return false;
}
答案 1 :(得分:1)
使用$.attr();获取任何元素的属性,并使用javascript replace()替换字符串..
$(function () {
function Delete(id) {
myConfirm('Do you want to delete this record ?',
function () {
// now you can use the variable "id" for 345...
alert('You clicked OK'); // It should be 345 here
},
function () {
alert('You clicked Cancel');
},
'Confirm Delete');
return false;
}
$('a.delete-button').click(function(){
var href = $(this).attr('href');
var id = href.replace('/Customer/Delete/', '');
Delete(id);
});
});
另一种可能性是数据属性:
<a class="delete-button'" href="/Customer/Delete/345" data-id="345">Delete</a>
上面的代码是:
$(function () {
function Delete(id) {
myConfirm('Do you want to delete this record ?',
function () {
// now you can use the variable "id" for 345...
alert('You clicked OK'); // It should be 345 here
},
function () {
alert('You clicked Cancel');
},
'Confirm Delete');
return false;
}
$('a.delete-button').click(function(){
Delete($(this).data('id'));
});
});
答案 2 :(得分:1)
在Delete
功能中:
var recordId = this.href.match(/\d+$/)[0];
答案 3 :(得分:0)
尝试使用jQuery data()
函数。这样,您就可以将数据绑定到元素,并从那里进行访问,而不是从URL中提取数据。