doufunuo4787 2013-02-16 03:55
浏览 45
已采纳

使用超链接在jquery中发送不同的值

i am working on a marketing project.there are some category like bank ,school and many other and i have the same table in my sql database..now i am wanted to show the result of which category user click .. my index.php

<?php
    $connection = mysql_connect('localhost', 'root', '');
    $db = mysql_select_db('market', $connection);
?>


<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js">
</script>
<script type="text/javascript">
$('document').ready(function () {
    $('a').click(function () {
        var temp = $('a').attr('myval');
        $.post('fetch.php', {
            id: temp
        }, function (data) {
            $('#display').html(data);
            var temp = null;
        });
    });
});
</script>
</head>
<body>
    <ul>
        <li>
            <a href="javascript:return(0)" myval="school">
                <h3>school</h3>
            </a>
        </li>;
        <li>
            <a href="javascript:return(0)" myval="bank">
                <h3>Bank</h3>
            </a>
        </li>;
        <!--many more......-->
    </ul>
    <div id="display"></div>
</body>

and its my fetch.php

<?php
$connection = mysql_connect('localhost', 'root', '');
$db         = mysql_select_db('market', $connection);
$x          = $_POST['id'];
$safex      = mysql_real_escape_string($x);

$query = mysql_query("select * from $safex", $connection);

$result = "";

$result .= "<div id='display'>";
$result .= "<table border=\"1\">";
$result .= "<tr><th>Name</th><th>Password(encrypted)</th></tr>";
while ($row = mysql_fetch_array($query)) {
    $result .= "<tr><td> {$row['id']}</td>" . "<td> {$row['name']}</td></tr></p>";
}
$result .= "</table>";

$result .= "</div>";

echo $result;

?>

and now my prblem is when i click on any link..its always show tie school data..but i want to show diffent table data on diffenent click..all fields name of all tables are same.. plz help....

  • 写回答

2条回答 默认 最新

  • douzhanrun0497 2013-02-16 03:56
    关注

    You need to use $(this) to get the attribute of clicked element.

    var temp = $(this).attr('myval');
    

    $(this) indicates on which the event was fired. Also as you are using anchor node, so, also use e.preventDefault() and also pass e argument in event handler function.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?