我正在做一个营销项目。有一些类别,如银行、学校和许多其他类别,我的 sql 数据库中有相同的表。现在我想显示用户点击哪个类别的结果。 我的 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>
还有我的 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;
?>
现在我的问题是当我点击任何链接时..它总是显示领带学校数据..但我想在不同的点击上显示不同的表格数据..所有表格的所有字段名称都相同.. 请帮忙....
最佳答案
你需要使用$(this)
来获取被点击元素的属性。
var temp = $(this).attr('myval');
$(this)
指示触发事件的位置。此外,由于您正在使用 anchor 节点,因此,也使用 e.preventDefault()
并在事件处理函数中传递 e
参数。
关于php - 使用超链接在 jquery 中发送不同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14906748/