jquery – 在href中存储POST请求的数据 – 不好的做法?

我正在编写node.js应用程序,我有点担心如何构建发送数据以发送到服务器.例如,当我想删除数据项时,我将所述项的id放在href属性中:

<a class='delete' href="1231">Delete this data</a>
<!-- href is based on a variable I'm pulling from the server -->

单击该链接时,我会阻止认操作,然后运行ajax请求:

//On click + ajax
body.on('click','.delete',function(e){
e.preventDefault();
    $.ajax({
        type: 'POST',url: '/admin/report/detail/delete',data: {id: $(this).attr('href')},success: function(){
        //Success message
    },error: function(){
        //Error message
    }
});
});

我想知道,以这种方式使用href属性是不好的做法吗?如果是这样,存储这些数据的最佳方法是什么?

解决方法

请改用数据属性.将它们存储在href中并不是语义的,如果您希望有时存储ID,以及其他时间存储其他数据,该怎么办?您可以根据需要创建任意数量的数据属性,为其提供语义名称.

<a class="delete" data-id="1231" href="#">

然后在你的JavaScript中:

...
data: { id: $(this).data('id') }
...

要么

data: { id: $(this).attr('data-id') }

相关文章

页面搜索关键词突出 // 页面搜索关键词突出 $(function () {...
jQuery实时显示日期、时间 html: &lt;span id=&quot...
jQuery 添加水印 &lt;script src=&quot;../../../.....
中文:Sys.WebForms.PageRequestManagerParserErrorExceptio...
1. 用Response.Write方法 代码如下: Response.Write(&q...
Jquery实现按钮点击遮罩加载,处理完后恢复 思路: 1.点击按...