du512053619 2015-02-05 07:24
浏览 22
已采纳

使用jQuery获取链接的属性并将其作为表单发布到另一个脚本文件中

I want to grab attributes of <a href> link and spit those attributes to another script file as post data. Following are the details of what I have done till now

Html link

<a href="#" class="submit-button" id="deleteRecord" attr1="xyz" attr2="abc" >Delete</a>

Now the jQuery code is as follows

$(document).ready(function(){
  //Other code here

  $(document).on('click','#deleteRecord',function(){
    //I want to submit variables attr1 and attr2 as post data to delete_post.php
  });
});

Thanks in advance.

  • 写回答

3条回答 默认 最新

  • dsfdsfdsfdsf1223 2015-02-05 07:50
    关注

    If you're unsure how many attributes you want to post, use data attribute and loop:

    <a href="#" class="submit-button" id="deleteRecord" data-something="blah" data-attr1="xyz" data-attr2="abc" >Delete</a>
    

    Script:

    $(document).ready(function(){
         $(document).on('click','#deleteRecord',function(e){
            e.preventDefault();
            var postData = {};
            $.each($(this).data(), function(k,v) {
                postData[k] = v;
            });
            $.ajax({
                type    : 'post',
                url     : 'delete_post.php',
                data    : postData,
                success : function (data) {
                    // do something with data...
                    console.log(data);
                },
                error   : function (obj,status,error) {
                    // do something when error...
                }
            });
        });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题