无法使用ajax单击其他页面回显的项目-$ {document).on无法正常工作 [英] cannot click items echoed by another page using ajax - $(document).on is not working

查看:87
本文介绍了无法使用ajax单击其他页面回显的项目-$ {document).on无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已更新

updated

我有2页.连接到js文件的索引页.该js文件包含从数据库中获取数据的ajax代码. 这是我的js文件

i'm having 2 pages. An index page connected to a js file. This js file containing ajax code fetching data from database. this is my js file

$(document).ready(function() {

    // getting links from db andshow sub_menu div //
    $(".menu_item").mouseover(function(){
        $(this).addClass("selected").children().slideDown(500,function(){
            var id = $(".selected").attr("id");
            var ajax= false;
            ajax = new XMLHttpRequest();
            var qst = "?id="+id;
            ajax.open("GET","ajax/get_sub_cats.php"+qst);
            ajax.onreadystatechange = function(){
                if(ajax.readyState == 4 && ajax.status == 200){
                    $(".sub_menu[title="+id+"]").html(ajax.responseText);
                }
            }
            ajax.send(null);
        });
    });

    // hiding sub_menu div //
    $(".menu_item").mouseout(function(){
        $(this).removeClass("selected").children(".sub_menu").slideUp(500);
    });

    // keeping sub_menu div visible on mouse over //
    $(".sub_menu").mouseover(function() {
        $(this).stop();
    });

    // clicking sub menu link in the menu //
    $(document).delegate("a#subCatLink","click",function(){
    alert("test");
    }); 
    // document ready end
    });

这是get_sub_cats php文件,用于从数据库获取链接

and this is get_sub_cats php file used to fetch links from db

<?php
require('../_req/base.php');
$id = $_REQUEST['id'];
$getSubcatsQ = "select * from sub_cats where Main_Cat_ID = '$id'";
$getSubcatsR = mysql_query($getSubcatsQ);
$numrows = mysql_num_rows($getSubcatsR);
while($row = mysql_fetch_array($getSubcatsR)){
    ?>
   <a id="subCatLink" href="products.php?id=<?php echo $row['Sub_Cat_ID']; ?>"><?php echo $row['Sub_Cat_Name']; ?></a><br />
    <?php
}
mysql_close($connect);
?>

使用ajax单击来自其他php文件的链接根本不起作用

clicking links coming from the other php file using ajax is not working at all

推荐答案

对不起,也许这会有所帮助,也许不会.但是...

Sorry, maybe this will help, maybe not. But...

你为什么不使用这样的东西:

Why don't you use something like this:

jQuery

$(".menu_item").mouseover(function(){   
    var id = $(".selected").attr("id");
    var qst = "?id="+id;
    var html = '';
    $.getJSON('ajax/get_sub_cats.php'+qst, function(data){
        var len = data.length;
        for (var i = 0; i< len; i++) {
            html += '<a id="subCatLink'+data[i].Sub_Cat_ID+'" href="products.php?id='+data[i].Sub_Cat_ID+'">'+data[i].Sub_Cat_Name+'</a>';
        }
        $(".sub_menu[id="+id+"]").html(html);
    });
});

PHP

require('../_req/base.php');
$return = array();

$id = $_REQUEST['id'];
$sql = "select * from sub_cats where Main_Cat_ID = '$id'";
$result = mysql_query($sql);

while($ln = mysql_fetch_array($result)){
    $return[] = $ln;
}

echo json_encode($return);

这篇关于无法使用ajax单击其他页面回显的项目-$ {document).on无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆