【发布时间】:2014-09-03 00:55:36
【问题描述】:
我在使用 ajax 时遇到了一点问题。
当我更改“select”标签的“option”值时,它会调用 ajax,而我从 ajax 调用中获得的值将显示在名为“t_name”的“input”标签中。但它不起作用。我的代码有什么问题?我该如何解决?谢谢。
下面是我的代码:
<select id="teacher_select" class="teacher_select" name="teacher_select" value="Select Teacher">
<option value="">Select Teacher</option>
<?php
while($row=mysql_fetch_assoc($teacher) ):
?>
<option value="<?php echo $row['t_id'];?>"><?php echo $row['t_name'];?></option>
<?php
endwhile;
?>
</select>
<input type="text" name="t_credit" id="t_credit"/>
阿贾克斯:
$('#teacher_select').click(function(){
$.ajax({
type:'post',
url:'get_taken_credit.php',
data: 't_id='+ $('#teacher_select').val(),
success: function(reply_data){
$('#t_credit').html(reply_data);
}
});
});
get_taken_credit.php:
include('db_connection.php');
$t_id = $_POST['t_id'];
$result = mysql_query("SELECT t_credit FROM teacher WHERE t_id = '$t_id'");
echo $result;
exit();
【问题讨论】:
-
不工作是什么意思?
-
您的 html 显示
#dept_select而您的 javascript 引用#teacher_select。您真的向我们展示了您的代码的正确 sn-p 吗?并且:尝试将$('#t_credit').html(reply_data);替换为$('#t_credit').val(reply_data);。 -
在下拉列表中也可以使用
change代替点击事件
标签: javascript php jquery ajax database