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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度