Php 在jquery中传递变量
我有下面的代码,我有第一个表,我通过while循环获取它的数据。我在这个表中有一行是“更多细节”,每行都有“细节”按钮。 我试过jquery的这段代码,但它只适用于第一个按钮, 假设我在表中有10行,当然有10个按钮,所以只有第一个按钮起作用并显示“table2”,但其他按钮不起作用。 我想也许我可以将一个变量传递给jquery,它可以确定用户单击哪个按钮来显示与此按钮相关的表2。 我已经用谷歌搜索过了,但是谷歌让我失望了,没有结果。 任何帮助都将不胜感激Php 在jquery中传递变量,php,jquery,Php,Jquery,我有下面的代码,我有第一个表,我通过while循环获取它的数据。我在这个表中有一行是“更多细节”,每行都有“细节”按钮。 我试过jquery的这段代码,但它只适用于第一个按钮, 假设我在表中有10行,当然有10个按钮,所以只有第一个按钮起作用并显示“table2”,但其他按钮不起作用。 我想也许我可以将一个变量传递给jquery,它可以确定用户单击哪个按钮来显示与此按钮相关的表2。 我已经用谷歌搜索过了,但是谷歌让我失望了,没有结果。 任何帮助都将不胜感激 <script src="htt
<script src="http://code.jquery.com/jquery-latest.js"></script>
<?php
$sql3= mysql_query("SELECT * FROM data ");
while($row3 =mysql_fetch_array($sql3)){
?>
<script>
$(document).ready(function() {
$('#showr').click(function(){
$('#Table2').show();
});
});
</script>
<table width='100%' border='1' cellspacing='0' cellpadding='0'>
<th>Weeks</th>
<th>date</th>
<th>place</th>
<th>More Details</th>
<tr>
<?php
echo "<tr ><td style= 'text-align : center ;'>my rows1</td>" ;
echo "<td style= 'text-align : center ;'>myrows2</td>";
echo "<td style= 'text-align : center ;'> myrows3</td>";
echo "<td style= 'text-align : center ;'><button id='showr'>More Details</button></td></tr>";
}
?>
</tr>
</table><br />
<div id= "Table2" style= "display:none;">
<table width='100%' border='1' cellspacing='0' cellpadding='0'>
<th>try</th>
<th>try2</th>
<tr>
<td>try3</td>
<td>trs</td>
</tr>
</table>
</div>
$(文档).ready(函数(){
$('#showr')。单击(函数(){
$('#表2').show();
});
});
周
日期
地方
更多细节
如果您想让每个按钮显示不同的表,我将使用ID在按钮和表之间创建关系。我假设您在表中使用了一个自动递增的主键;如果没有,您可以在循环中放置一个计数器,并将其用作id
下面省略了许多输出有效表的代码
<?php
while($row3 = mysql_fetch_array($sql3)){
//output your normal table rows
//presuming a numeric primary key to use as id
echo "<td><button id='showr_" . $row3['primaryKey'] . "' class='showr'>Show Details</button></td>";
}
?>
<?php
//reset mysql data set so we can loop through it again to output the second tables
mysql_data_seek($sql3, 0);
while($row3 = mysql_fetch_array($sql3)){
//output hidden table
echo "<table style='display: none' class='table2' id='table2_" . $row3['primaryKey'] . "'>";
//output rest of rows here...
echo "</table>";
?>
此代码将给出一个PHP解析错误。这是您的实际代码还是您尝试拼凑了一个示例?是的,这是我自己的代码,很抱歉PHP标记现在放错了位置。是的,PHP标记是您最小的问题。是的,这适用于所有按钮,但它们在“表2”中提供相同的数据,如何让每个按钮在“表2”中给出不同的数据哦,我误解了你的问题。实现多个table2的一种方法是将每个唯一的“table2”输出为一个隐藏表,每个表都有一个唯一的id(table2_5)。然后每个按钮都可以有相应的id(showru 5)。我将编辑我的答案。
<script type='text/javascript'>
$(document).ready(function() {
$('.showr').click(function(){
//get id by splitting on the underscore within the 'id' attribute
//$(this) refers to the button that has been clicked
var id = $(this).attr('id').split('_')[1];
//hide all table2's and then show the one we want
$('.table2').hide();
$('#Table2_' + id).show();
});
});
</script>