dtgsl60240 2014-05-27 08:54
浏览 123
已采纳

用链接调用ajax函数

I am trying to create an ajax function with all the data inside the following variable inside the following function: $item

public function onK2AfterDisplayContent(&$item,&$params,$limitstart)
{
    $view = JRequest::getVar('view');

    if($view === 'item'){
        $db = JFactory::getDbo();
        $query = 'SELECT name FROM #__k2_categories WHERE id='.$item->catid;
        $db->setQuery($query);
        $resultado = $db->loadResult();

        echo '<input class=subscribete type=button id='.$resultado.'-'.$item->id.' value="Subscribete" />'; 
    }
}

Im not sure if instead of using a <button> I should use an html link. The variable $item is an object with a lot of information which I want to take to my AJAX for later treating this data...

  • 写回答

2条回答 默认 最新

  • doupaxia2478 2014-05-27 09:59
    关注

    You can use either the anchor or the button element, it depends on your requirement. User always expect web page will direct to another page or popup something after click anchor and trigger a action (form submit, show/hide content...etc) after click a button. Please note you should call event.preventDefault() in click event handler in order to avoid appending unnecessary "#" to url when you're using anchor element as a AJAX caller.

    Then you need to store the AJAX call required data to DOM by using custom data attribute in PHP. (Assume that you only need the ID of $item)

    public function onK2AfterDisplayContent(&$item,&$params,$limitstart)
    {
      $view = JRequest::getVar('view');
    
      if($view === 'item'){
        $db = JFactory::getDbo();
        $query = 'SELECT name FROM #__k2_categories WHERE id='.$item->catid;
        $db->setQuery($query);
        $resultado = $db->loadResult();
    
        //add required data to DOM
        echo '<input class="subscribete" type="button" id="'.$resultado.'-'.$item->id.'" value="Subscribete" data-id="'.$item->id.'"/>'; 
      }
    }
    

    Final step, register the click event handler of DOM which will trigger a AJAX call.

    $(function(){
      $(".subscribete").click(function(e){
        if(this.tagName==="A"){
          e.preventDefault();
        }
    
        //$(this).data("key") will return the value at the named data which is set by HTML5 data-* attribute
        $.ajax({
          url: "http://path.to.ur/url",
          type: "POST",
          data: {id:$(this).data("id")},
          success: function(response){
            alert(response);
          },
          error: function(jqXhr,status,error){
            alert(error);
          }  
        });
      });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?