如果选项值已存在,请禁用选择选项
disable select option if the option value is already exist
我正在创建一个图书馆成员表单,在此表单中,通过选择选项中的 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 代码
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);
}
});
}
拉取学生列表的控制器
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;
}
但是上述控制器将遍历所有学生和分配的学生,并在禁用和非禁用格式上输出相同的学生选项。那么,我如何防止此控制器显示所有学生都是已经是会员的残疾学生。
上述控制器的输出
您可以在数组中收集分配的学生 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;
相关文章:
- 使用此选项选择父类内部的类
- 更新:仅根据单选按钮和所选选项选择特定项目
- 使用PHP通过HTML表单选项选择器过滤MYSQL结果
- ng-单击在IE中不起作用的选项选择Angularjs
- 从选项选择中删除值
- On为<选项>选择等效项
- 用户界面先前选择的选项选择更改
- 使用ng重复或ng选项和删除空白选项选择长方体角度
- 在HTML5、CSS、JavaScript中添加选项选择器,类似于移动应用程序中的选项设置
- 输入:选中,选项:选择选择器
- 根据使用angularjs从下拉菜单中选择的选项选择适当的复选框
- 如何根据 html 中的选项/选择重定向用户(单击按钮后)
- 使用ng选项选择中的ng选项进行过滤器下拉列表
- 如何设置选项选择的值
- 如何搜索长下拉选项/选择列表
- jQuery - 根据下拉选项选择列出结果
- 从选项选择单击时打开 2 个 URL
- 在 Angular JS 中为 ng 选项选择默认值
- 下拉选项选择的操作
- 通过单击单选按钮清除所有选项选择条目