【问题标题】:Running Ajax within bootstrap在引导程序中运行 Ajax
【发布时间】:2016-01-08 13:44:27
【问题描述】:

我正在开发 CMS,主题的主要核心是引导程序。我的问题出现在 Ajax 上。我有我的登录模块,它可以很好地登录和运行 ajax,但是当我包含 ajax 库时,我的下拉菜单的问题不起作用。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>

<script src="http://www.bootstrapcdn.com/twitter-bootstrap/2.2.1/js/bootstrap.min.js"></script>

这些是我包含的用于运行 ajax 的库,以及我的 ajax 代码 加上我正在使用的表格

<form name="panel_login" id="panel_login" method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="hidden" name="process" id="process" value="panel_login">
<p><label for "username">Username</label>
<input type="text" id="username" name="username" class="text"></p>
<p><label for "password">Password</label>
<input type="password" id="password" name="password" class="text"></p>
<p>
<p id="error"></p>
<input type="submit" id="submit_login" class="button" value="Login"></p>
</form>
<script>
$(function() {
  $('#submit_login').click(function(event) {
    $.ajax({
      type: $("#panel_login").attr("method"),
      url: $("#panel_login").attr('action'),
      data: $("#panel_login").serialize(),
        success: function(data){
            if(data == "0" ) {
                $("#error").html("Incorrect Username or Password");
            }
            else {
                location.reload();
            }
        }
    });
    event.preventDefault(); 
    });
});
</script>

这个过程是在一个可以正常工作的案例开关中抛出的,此时唯一的问题是下拉菜单损坏。

下拉菜单代码:(nav.dropdown.php)

       <li class="dropdown">
                <a href="<?php echo $this->dlink; ?>" class="dropdown-toggle" data-toggle="dropdown"><?php echo $this->dtitle; ?> <b class="caret"></b></a>
                <ul class="dropdown-menu">
                <?php $perms = $this->perms; ?>
                <?php self::GetChildren($perms); ?>
                </ul>

这是通过 PHP 函数文件包含的

class DViews {
    protected $db;
    public $tprefix;
    public $theme;
    public function __construct() {
        $this->db = new DDB();
        $this->tprefix = TPREFIX;
        $this->theme = theme_p;
    }
    public function LoadNavigation($perm, $menu) {
        switch($perm) {
            case "all";
            self::AllNavigation($perm, $menu);
            break;
            case "user";
            self::AllNavigation($perm, $menu);
            break;
            case "guest";
            self::AllNavigation($perm, $menu);
            break;
        }
    }
    public function AllNavigation($perm, $menu) {
        $query = <<<SQL
        SELECT id,title,content,menuid,identifier
        FROM {$this->tprefix}pages
        WHERE active = :true
        AND visibility = :all
        AND menuid = :menu
SQL;
        $resource = $this->db->db->prepare( $query );
        $resource->execute( array (
        ':true' => 1,
        ':all'  => $perm,
        ':menu' => $menu,
        ));
        foreach($resource as $row)
        {
            $this->perms = $perm;
            $this->id = $row['id'];
            $this->title = $row['title'];
            $this->identifier = $row['identifier'];
            self::CheckParent($perm);
        }
    }
    public function CheckParent($perm) {
        $query = <<<SQL
        SELECT parent
        FROM {$this->tprefix}pages
        WHERE parent = :id
        AND active = :1
        AND visibility = :perms
SQL;
        $resource = $this->db->db->prepare( $query );
        $resource->execute( array (
        ':id'   => $this->id,
        ':1'    => 1,
        ':perms'    => $perm,
        ));
        if(PRETTYURLS == true) {
        $this->dlink = $this->identifier;
        }
        else {
            $this->dlink = "?id=".$this->identifier."";
        }
        $this->dtitle = $this->title;


            if($resource->rowCount() > 0 ) {
                include($this->theme.'/'.ACTIVETHEME.'/nav.dropdown.php');
            }
            else {
                include($this->theme.'/'.ACTIVETHEME.'/nav.single.php');
            }
    }
    public function GetChildren($perm) {
        $query = <<<SQL
        SELECT id,title,content,menuid,identifier
        FROM {$this->tprefix}pages
        WHERE active = :true
        AND visibility = :all
        AND parent = :parent
SQL;

        $resource = $this->db->db->prepare( $query );
        $resource->execute( array (
        ':true' => 1,
        ':all'  => $perm,
        ':parent'   => $this->id,
        ));
        foreach($resource as $row)
        {
        $this->title = $row['title'];
            $this->dtitle = $this->title;
            $this->identifier = $row['identifier'];
        if(PRETTYURLS == true) {
        $this->dlink = $this->identifier;
        }
        else {
            $this->dlink = "?id=".$this->identifier."";
        }

            include($this->theme.'/'.ACTIVETHEME.'/nav.single.php');
        }
    }

尝试添加

var $j = jQuery.noConflict();


$j(function() {
  $j('#submit_logout').click(function(event) {
    $j.ajax({
      type: $("#panel_logout").attr("method"),
      url: $("#panel_logout").attr('action'),
      data: $("#panel_logout").serialize(),
        success: function(data){
        location.reload();
        }
    });
    event.preventDefault(); // Prevent the form from submitting via the browser.
    });
});

无济于事。

【问题讨论】:

  • 您说的是哪个下拉菜单??
  • 我将编辑并包含我用于他们的代码。
  • 你知道你可以通过回车发送一个表单,所以点击事件永远不会被执行?您应该使用提交,而不是捕获点击
  • 是的;正在使用点击参数,但它仍然在点击和提交下进行处理,但这是一个简单的更改,根本不会中断流程。
  • 好吧。将其更改为首先在注销时提交,它实际上作为点击功能效果更好。在提交功能下,它将显示用户的登录功能,直到您导航到新页面,而单击它会自动更改信息。感谢您的意见,但我想我会保持原样。

标签: php jquery ajax twitter-bootstrap


【解决方案1】:

不知道为什么它会起作用,但我在数百万个引导论坛上偶然发现了一个似乎就在那里的决议。 链接到外部

<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>

似乎已经解决了 Jquery 和 Bootstrap 冲突的问题,而不是 local

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-23
    • 2020-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多