【问题标题】:Ajax call after changing the value of a 'option' tag更改“选项”标签的值后的 Ajax 调用
【发布时间】: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


【解决方案1】:

好像id不对,你叫#teacher_select而不是#dept_select,试试用

$('#dept_select').change(function () {  

而不是

$('#teacher_select').click(function () {

【讨论】:

  • 请用英语。看不懂@Mi名字
  • 谢谢 Ehsan Sajjad,我不会说英语
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-14
  • 1970-01-01
  • 1970-01-01
  • 2021-12-20
相关资源
最近更新 更多