我有两个选择框值: Sales Manager 1 和 Sales Manager 2 。我需要在 Sales Manager 2 中隐藏 Sales Manager 1 的选定选项值。这两个选项均从数据库获取值。我该如何实施?
查看文件:
<div class="form-group">
<label for="sales">Sales Manager 1</label>
<select name="erp_customer[manager_id]" id="manager_id" class="form-control select2" data-validation="required" data-validation-error-msg="Sales Manager is required" placeholder="Select Manager">
<option value="">Select Sales Manager</option>
<?php
foreach ($sale_manager as $mg) { ?>
<option value="<?php echo $mg->manager_id; ?>" <?php echo (!empty($customer) && $customer->manager_id == $mg->manager_id) ? 'selected' : ''; ?>><?php echo ucfirst(mb_strtolower($mg->sale_fname)); ?></option>
<?php } ?>
</select>
</div>
<div class="form-group">
<label for="sales">Sales Manager 2</label>
<select name="erp_customer[manager_id1]" id="manager_id2" class="form-control select2" data-validation="required" data-validation-error-msg="Sales Manager is required" placeholder="Select Manager">
<option value="">Select Sales Manager</option>
<?php
foreach ($sale_manager as $mg) { ?>
<option value="<?php echo $mg->manager_id; ?>" <?php echo (!empty($customer) && $customer->manager_id1 == $mg->manager_id) ? 'selected' : ''; ?>><?php echo ucfirst(mb_strtolower($mg->sale_fname)); ?></option>
<?php } ?>
</select>
</div>
选项值将类似于1、2、3、4。我从另一个表中获得名称,其中1="Ram"
2="ragu"
,所以如何在选择了选项值1后在销售经理2中隐藏它。我尝试了这段代码,但仅对第一次选择有所帮助。一旦我尝试再次选择,只有第一次选择的值被禁用。
$('.form-control[id=manager_id]').on('change', function(e){
var thisVal = $(this).val();
$('.form-control[id=manager_id2] option').each(function(){
if(thisVal == $(this).attr('value')){
$(this).attr('disabled', 'disabled');
} else {
$(this).removeAttr('disabled');
}
})
})
我的控制器:
public function addcustomer(){
$data["title"] = "Add New Customer";
$data["sale_manager"] = $this->customer_model->getsales_managerlist();
load_default_template('customer/addnew',$data,$this);
echo $this->template->render("", true);
}
我的模特:
public function getsales_managerlist(){
$this->db->select("*")->from('erp_manager')->where("status", 1);
$query = $this->db->get();
return $query->result();
}
答案 0 :(得分:0)
我认为您真的不需要在循环中执行else
来禁用它。只需默认将其删除,然后将其重新设置即可(如果值匹配)。这是以下脚本的演示:https://jsfiddle.net/wf82Lc3j/
$(function(){
$(this).on('change', '.form-control[id=manager_id]', function(e){
// Get current value
var thisVal = $(this).val();
// Get the second select obj
var thatObj = $('.form-control[id=manager_id2]');
// Loop options
$.each(thatObj.children(), function(k,v){
// Just remove disabled right off the start
$(v).attr('disabled', false);
// If value equals current
if($(v).attr('value') == thisVal) {
// If not on the default select option
if(thisVal != '') {
// Disable the option
$(v).attr('disabled', true);
// If the user has already select this item before selecting from the first menu
if($(v).is(":selected")) {
// Reset the selection to default
thatObj.val('');
}
}
}
});
});
});