简体   繁体   English

jQuery无法正常运行

[英]jQuery not functioning properly

When i select a value from the select box i need to get the corresponding values on the other field.this is my table 当我从选择框中选择一个值时,我需要在另一个字段上获取相应的值。这是我的表

     id     first_name  last_name      stud_id      dob         hostel   class  room  bed   status
    175     siraj         k           WPGH00175   01-10-2016      7       28    41    2A    P
    176     nesru         kv          WPGH00176   31-10-2016      7       28    41    2B    P
    177     faizal        ep          WPGH00177   31-10-2016      7       28    41    2C    P
    179     mashoor       uv          WPGH00179   24-10-2016      7       28    41    2D    G

This is my view page 这是我的查看页面

<div class="form-group" id="stud_id">
          <label for="student" class="col-sm-4 control-label">Student ID</label>
          <div class="col-sm-5">
            <select class="chosen-select chosen-transparent form-control" name="student" id="student">
             <option value="">--Select Student-- </option>
            <?php foreach ($students  as $row) { ?>
              <option value="<?php echo $row->id; ?>"><?php echo $row->stud_id; ?></option>
              <?php }?>
            </select>
          </div>
        </div>
        <div class="form-group">
          <label for="input01" class="col-sm-4 control-label">First Name:</label>
          <div class="col-sm-5" >
            <input type="text" class="form-control" id="first_name1" name="first_name" value="">
          </div>
        </div>

This is my jQuery 这是我的jQuery

 <script type="text/javascript">
 $(document).ready(function(){

   $('#student').change(function(){ 
      var stud_id=$('#student').val();

      var url='<?php echo base_url(); ?>hostel/ajax_data';

      $.post(url,{s_id:stud_id}, function(data)
          {
        //alert();
             $("#first_name1").html(data);
          });
        });
     });
    </script>

This is my control 这是我的控制

public function ajax_data()
{
  $stud_id = $_POST['s_id'];
  $data['result'] = $this->admin_model->ajax_name($stud_id);
  $this->load->view('hostel/ajax_data',$data);
}

This is my model 这是我的模特

public function ajax_name($stud_id)
{
    $query=$this->db->get_where('student_hostel',array('id'=>$stud_id))->row();
    return $query;
}

when i give val instead of html the entire which i had given in view is showing. 当我给出val而不是html时,显示的是我给出的全部。 this is my view 这是我的看法

<input type="text" class="form-control" id="first_name1" name="first_name" value="<?php echo $result->first_name;?>">

In view (hostel/ajax_data) you are printing whole html input and trying to place it again inside already loaded input using jquery- which doesn't make much sense. 在视图(hostel / ajax_data)中,您正在打印整个html输入,并尝试使用jquery将其再次放入已加载的输入中,这没有太大意义。 You can only echo 你只能回声

<?php echo $result->first_name;?> 

in view or directly in controller and using jquery put this response in the loaded input like this: 在视图中或直接在控制器中并使用jquery将此响应放入已加载的输入中,如下所示:

$("#first_name1").val(data);

If you need more data, not only first name, in that case you can make json. 如果您需要更多数据,不仅是名字,在这种情况下,您可以制作json。

Example: 例:

Change in Model: 型号变更:

public function ajax_name($stud_id)
{
    $query=$this->db->get_where('student_hostel',array('id'=>$stud_id))->row_array(); //use row_array instead of row() to return an array
    return $query;
}

Change in Controller: 控制器变更:

public function ajax_data()
{
  $stud_id = $_POST['s_id'];
  $result = $this->admin_model->ajax_name($stud_id);
  echo json_encode($result); //converting the array to json string and outputting it directly instead of using view
 }

Change in jQuery: 更改jQuery:

<script type="text/javascript">
 $(document).ready(function(){

   $('#student').on("change",function(e){ 
      var stud_id = $(this).val();

      var url='<?php echo base_url('hostel/ajax_data'); ?>';
     $.ajax({
        url: url,
        type: "post",
        data:{s_id: stud_id},
        success: function(result){
            var data = JSON.parse(result); //parsing server response to JSON object
            // Now data is an object. As your query in model is returning all fields, you can also use other fields like in this way: data.last_name, data.dob etc
            $("#first_name1").val(data.first_name);  //updating the input field with retrieved value. This way you also update other fields if necessary
        }
      });

   });
 });
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM