我有一个下拉选择PHP页面,使用jQuery,AJAX和MySQL在选择器被改变,产生一个表,并放到一个div叫.mytable内任何链接的选择。表中的一些数据是影响表中数据的php脚本的链接,所以我想刷新表而无需重新加载。什么是表
我知道我需要一个事件监听器安装在表中的链接要做到这一点,但我似乎无法得到正确的选择到听者重视?
任何帮助,将不胜感激
用户看到的页面,被称为user.php的是:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<link href="css/table_style.css" rel="stylesheet" type="text/css">
</head>
<body>
<form name="form1" method="post" action="">
<label for="select_data"></label>
<select name="select_data" id="select_data">
<option value=1>"Apples"</option>
<option value=2>"Pears"</option>
<option value=3>"Bananas"</option>
</select>
</form>
<div id="mytable">This is where the table goes</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="get_table.js"></script>
</body>
</html>
jQuery的事件处理程序是:
$(document).ready(function(){
//listen for select list to change, pass fruit into create_table.php, pass data into mytable div
$('#select_data').change(function() {
$.post('create_table.php',{fruit: fruit}, function(data) {
$('div#mytable').html(data);});
});
$('.fruit_link').change(function() {
$.post('create_table.php',{fruit: fruit}, function(data) {
$('div#mytable').html(data);});
});
});
的脚本事件创建表并返回html代码的句柄调用是:
<?php
require_once('Connections/dbc.php');
$fruit=$_POST['fruit'];
$sql="SELECT * FROM q_fruit WHERE FruitID =".$fruit;
$rec1=mysqli_query($dbc,$sql);
echo '<table class="table" align="center">';
echo '<th class="th" width="80px">Fruit ID</th>';
echo '<th class="th" width="150px">Fruit Name</th>';
echo '<th class="th" width="40px">Fruit Action 1</th>';
echo '<th class="th" width="150px">Fruit Action 2</th>';
while ($row=mysqli_fetch_array($rec1))
{
echo '<tr class="tr">';
echo '<td class="td">'.$row['FruitID']. '</td>';
echo '<td class="td">'.$row['FruitName']. '</td>';
echo '<td class="td"><a class="fruit_link" href="fruitaction1.php">'.$row['FruitID']. '</a></td>';
echo '<td class="td"><a class="fruit_link" href="fruitaction2.php">'.$row['FruitID']. '</a></td>';
echo '</tr>';
}
echo '</table>';
mysqli_free_result($rec1);
mysqli_close($dbc);
?>
所以,选择事件处理程序的每一次的值发生改变时再现表,但链接的处理程序是没有的,我假定它是因为链接是在HTML多数民众赞成在返回的,而不是已经存在java正在监听的用户文件。有没有办法解决这个问题?
'$( 'MyTable表TR TD一')。点击(函数(){})'应该这样做 – asprin
在这里发表您尝试过的代码。 –
这可以很容易地通过不要使用AJAX和检查这个http://stackoverflow.com/questions/5681380/refresh-a-table-with-jquery-ajax-ever-5-seconds – user2504141