doujia9833 2016-02-27 05:45
浏览 21

使用AJAX JQuery发送POST数据

I want to send the selected item from Bootstrap dropdown in POST to my controller, but somehow it is not being sent.

In my dropdown I fetch records from the database in <li></li> tag, like this:

 <li class="dropdown">

      <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Your Sites <span class="caret"></span></a>
      <ul class="dropdown-menu">
        <li ><a href="#"><?php

    foreach($sites as $site)
    {

    echo "<li class='specialLink' id='$site->site_key'>".$site->site_key."</li>";
    }?></a></li>

      </ul>
    </li>     

This is my script to send POST data:

<script type="text/javascript">

$( ".specialLink" ).click(function() {
    var value = this.id; //get value for throw to controller
    alert(value);  

    $("#specialLink").submit(function(){
        $.ajax({
            type: "POST", //send with post
            url: "<?php echo site_url('customer/dashboard') ?>",  
            data: "value=" + value,
        });
    });
});

</script>

But I don't send anything in POST data at my controller, nor do I get any error message in the console.

  • 写回答

2条回答 默认 最新

  • donglun4521 2016-02-27 05:53
    关注

    I think you're not using submit correctly. The submit function binds an event handler to the submit javascript event. Read more at https://api.jquery.com/submit/.

    You just want to do the ajax request when $(.specialLink) is clicked, so try:

     $( ".specialLink" ).click(function() {
        var value = this.id; //get value for throw to controller
        $.ajax({
          type: "POST", //send with post
          url: "<?php echo site_url('customer/dashboard') ?>",  
          data: "value=" + value, 
        });
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题