我在使用ajax时遇到了一个小问题。
我需要当我改变'选择'标签的'选项'的值,然后它会调用ajax和我从ajax调用得到的值将显示在'输入'标签名为't_name'。但这行不通。我的代码有什么问题?我怎么解它?谢谢你。
下面是我的代码: <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"/>
Ajax: $('#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();
似乎id是错误的,您调用#teacher_select
而不是#dept_select
,并尝试使用
$('#dept_select').change(function () {
代替
$('#teacher_select').click(function () {