【问题标题】:Not working plugins with ajax无法使用 ajax 的插件
【发布时间】:2014-01-08 03:34:56
【问题描述】:

此代码有一个 ajax 脚本,用于删除记录并在删除时有动画。但是当我将 ajax 脚本与插件混合时,确认它不起作用。这个脚本有什么问题?

这是我的表格记录中的删除按钮。

<?php
echo'   <tr class="record"> 
    <td align="center"><a href="#" id="'.$row["counter"].'" class="delbutton"><img src="images/del.png"></a></td></tr>';
?>

带有确认插件的 Ajax

  <script>
      $(function() {
        $(".delbutton").click(function(){
            var element = $(this);
            var del_id = element.attr("id");
            var info = 'id=' + del_id;
            $(function() {
    $("<div />").attr("id", "dialog-confirm").append(
        $("<p />").text('Are you sure?').css("text-align", "center").prepend(
            $("<span />").addClass("ui-icon ui-icon-alert").css({
                display: 'inline-block',
                margin: '0px 7px 0px 0px'
            })
        )
    ).dialog({
                  resizable: false,
                  height:140,
                  modal: true,
                  buttons: {
                    "Delete all items": function() {                           
                       $.ajax({
                         type: "GET",
                         url: "delete.php",
                         data: info,
                         success: function(){
                            $(this).parents(".record").animate({ backgroundColor: "#fbc7c7" }, "fast").animate({ opacity: "hide" }, "slow");
                         }
                       }); 
                       $( this ).dialog( "close" );
                    },
                    Cancel: function() {
                      $( this ).dialog( "close" );
                      return false;
                    }
                  }
                }); // end Dialog         
            return false;
        }); // end .delbtn
        });
    }); // end jquery load
  </script>

【问题讨论】:

  • 这个&lt;script&gt; 标签和你的&lt;script&gt; 标签的其余部分在哪里?如果在&lt;head&gt; 标签中,那么您应该使用$(document).ready()。如果您的插件来自另一个非 jQuery 库,请使用 $.noConflict() api.jquery.com/jQuery.noConflict 。

标签: javascript php jquery ajax


【解决方案1】:

我复制您的代码并进行测试,它很有用!所以,请检查文件顺序,我喜欢这样:

 <script src="//code.jquery.com/jquery-1.9.1.js"></script>
 <script src="//code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
 <link href="//code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" rel="stylesheet" />

【讨论】:

  • 有时它可以工作,但是当它执行失败时,此消息在控制台中 TypeError: $(...).attr(...).append(...).dialog is not a function
  • 你有导入库吗?
  • 嗯..创建一个新文件并在新文件中设置清晰的代码试试吧,我只能提供帮助,请逐步调试
猜你喜欢
  • 1970-01-01
  • 2011-02-05
  • 1970-01-01
  • 2021-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-20
  • 1970-01-01
相关资源
最近更新 更多