如果选项值已存在,则禁用选择选项

时间:2016-01-24 05:31:43

标签: javascript jquery ajax codeigniter

我正在创建一个库会员表单,在此表单中,学生列表通过select选项中的ajax请求填充。现在我需要禁用已经是图书馆成员的学生选项。

表单视图

<div class="form-group">
  <div class="input-group">
    <span class="input-group-addon"><i class="fa fa-asterisk" aria-hidden="true"></i> <i class="fa fa-calendar" aria-hidden="true"></i>  <?php echo get_phrase('_batch'); ?></span>
    <select id="batch_result_holder" name="batch_result_holder" onchange="return get_batchs_students(this.value)" data-plugin="select2" required>
      <option disabled selected value=""> <?php echo get_phrase('select_programs_first'); ?></option>
    </select>
  </div>
</div><!-- form-group -->

<div class="form-group">
  <div class="input-group">
    <span class="input-group-addon"><i class="fa fa-asterisk" aria-hidden="true"></i> <i class="fa fa-user" aria-hidden="true"></i>  <?php echo get_phrase('_student'); ?></span>
    <select id="student_result_holder" name="student_result_holder"  data-plugin="select2" >
      <option disabled selected> <?php echo get_phrase('select_batch_first'); ?></option>
    </select>
  </div>
</div><!-- form-group -->

AJAX CODE拉取学生资讯

function get_batchs_students(batch_result_holder){
  var program_id = $('#program_id').val();
  $.ajax({
    type:"POST",
    url: '<?php echo base_url();?>index.php?admin/get_batch_students_without_assigned/',
    data:{batch_result_holder: batch_result_holder, program_id:program_id},

    success: function(response)
    {
      jQuery('#student_result_holder').html(response);
    }
  });
}

拉动学生列表的CONTROLLER

function get_batch_students_without_assigned($program_id, $batch_id, $status){
  $program_id = $this->input->post('program_id');
  $batch_id = $this->input->post('batch_result_holder');
  $students = $this->crud_model->get_student_list_without_section($program_id, $batch_id, 1);
  $assigned_student = $this->crud_model->get_assigned_students();
  echo '<option value="" selected disabled> select from list below </option>';
  foreach($assigned_student as $row2):
    foreach($students as $row){

      echo '<option value="' . $row['student_id'] . '"';
      if($row['student_id'] == $row2['type_id']):
        echo 'disabled';
      endif;
      echo '>';
      echo $row['name'];
      echo '</option>';
    }
  endforeach;
}

但是上述控制器将遍历所有学生并指派学生并输出相同的学生选项,包括禁用和非禁用格式。那么,我如何阻止该控制器显示所有学生都是已经是会员的残疾学生。

output of the above controller

1 个答案:

答案 0 :(得分:0)

您可以在数组中收集已分配的学生ID,并检查循环中的student_id是否在数组中:

  $assigned = array();
  foreach($assigned_student as $row2) {
    $assigned[] = $row2['student_id'];
  }
  foreach($students as $row){}
      echo '<option value="' . $row['student_id'] . '"';
      if(in_array($row['student_id'], $assigned):
        echo 'disabled';
      endif;
      echo '>';
      echo $row['name'];
      echo '</option>';
  endforeach;