weixin_33744141 2015-05-28 09:00 采纳率: 0%
浏览 12

jQuery Ajax通过链接发布

I am trying to post via link. But I think there is a problem. I am not good at Javascript. I want to send attribute and show with div.

Here is my Code :

  <script type="text/javascript">
      $(document).ready(function() {
          $("#slidingProduct").click(function() {
              var aa = $(this).attr('urun_id');
              $.ajax({
                  url: "data.php",
                  dataType: "POST",
                  data: {
                      "number1": aa
                  },
                  success: function(json) {
                      $("#result").html(json.number1);
                  }
              });
          });
      });
  </script>
  <a href="#" id="slidingProduct" urun_id="apple">A</a>
  <a href="#" id="slidingProduct" urun_id="banana">B</a>
  <a href="#" id="slidingProduct" urun_id="orange">O</a>
  <div id="result"></div>
  • 写回答

2条回答 默认 最新

  • csdnceshi62 2015-05-28 09:06
    关注

    You can do something like this:

    $(".slpd").on('click',function(){
          var aa = $(this).data('urun_id'); 
          var json={"number1":aa};
          $.ajax({
                url: "data.php", 
                type: "POST", 
                dataType:'JSON',
                data: JSON.stringify(json), 
                success: function(result){ 
                    $("#result").html(result.number1); 
                }
          });
    });
    

    As ids in DOM should be unique, you can specify similar class name to capture click event in a single go and some modifications in html - It's good to use data-* property of html5:

    <a href="#" id="slidingProduct1" class="slpd" data-urun_id="apple">A</a>
    <a href="#" id="slidingProduct2" class="slpd" data-urun_id="banana">B</a>
    <a href="#" id="slidingProduct3" class="slpd" data-urun_id="orange">O</a>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上