select的option

最近再做一个新的东西,有一个需求是这样的,后台给过来一批数据,这批数据要放到三个select里面,提供选择;但是第一个选框选过的内容后面就不能再选了,可以隐藏,可以置灰,可以移除。。。。。。。。。

最初选择了隐藏的方法,思路是在change事件的时候先让所有的选项全部显示,然后得到另外两个select里面选中的value值,然后隐藏,把最新的也从option里面隐藏,实现方式如

    $select.change(function(){
            //获取当前选中的选项
            var seloption = $(this).find(‘option:selected‘).attr(‘data-index‘),
                $other_select = $(‘select.sel-question‘).not($(this)),
                index1,index2;
            //显示所有的dom结构
            $(‘option‘,$select).show();
            //拿到另外两个框里面选中的
            index1 = $other_select.eq(0).find(‘option:selected‘).attr(‘data-index‘);
            index2 = $other_select.eq(1).find(‘option:selected‘).attr(‘data-index‘);
            //如果另外的第一个框里面有值,隐藏这个值的dom结构
            if(index1 > 0){
                $(‘option.option‘+ index1 +‘‘).hide();
            }
            if(index2 > 0){
                $(‘option.option‘+ index2 +‘‘).hide();
            }
            //删除选中的dom结构
          $(‘option.option‘+ seloption +‘‘).hide();
    });

但是测试结果是谷歌ok,火狐ok,ie就挂掉了。这是为什么呢?度娘告诉我select的option不能实现隐藏啊,改变策略吧,改成置灰,实现思路一致不过就是改变一下写法,改写成下面这样

$select.change(function(){
	//获取当前选中的选项
	var seloption = $(this).find(‘option:selected‘).attr(‘data-index‘),
		$other_select = $(‘select.sel-question‘).not($(this)),
		index1,index2;
	//显示所有的dom结构
	$(‘option‘,$select).attr(‘disabled‘,false);
	//拿到另外两个框里面选中的
	index1 = $other_select.eq(0).find(‘option:selected‘).attr(‘data-index‘);
	index2 = $other_select.eq(1).find(‘option:selected‘).attr(‘data-index‘);
	//如果另外的第一个框里面有值,隐藏这个值的dom结构
	if(index1 > 0){
		$(‘option.option‘+ index1 +‘‘).attr(‘disabled‘,‘disabled‘);
	}
	if(index2 > 0){
		$(‘option.option‘+ index2 +‘‘).attr(‘disabled‘,‘disabled‘);
	}
	//删除选中的dom结构
	$(‘option.option‘+ seloption +‘‘).attr(‘disabled‘,‘disabled‘);
});

  这下ok了,测试没有问题浏览器兼容的。哎,看来是自己知道的太少,急需积累啊

select的option

时间: 2024-07-28 14:00:24

select的option的相关文章

有关attribute和property,以及各自对select中option的影响

这个问题老生常谈,但是直到现在我依旧时常会把它搞混.下面列一些各自的特性.   attribute property 设置方法 option.setAttribute('selected', true) option.getAttribute('selected') option.selected = true dom节点表现 会表现在html节点上.打开控制台,可以看到 <option selected=true></option> 不会表现在html中.打开控制台,孤零零的 :

js 操作select和option

js 操作select和option 1.动态创建select function createSelect(){ var mySelect = document.createElement_x("select");          mySelect.id = "mySelect";           document.body.appendChild(mySelect);      } 2.添加选项option function addOption(){ //根

AngularJS下拉列表select在option动态变化之后多出了一个错误项的问题

场景: Select初始化之后,选中select的某个选项 通过AngularJS更新select的选项 错误写法: HTML(使用ng-repeat) <div ng-app="TestApp"> <div ng-controller="TestAppCtrl"> <label>options变化之后会出错:</label> <select ng-model="selectedSite"&g

jquery为select添加option的代码探讨

这是一道讨论"使用jQuery为select添加option选项的最佳代码方法".分析哪一种的写法是最佳方法.在stackoverflow上众说纷纷,下面来看看有哪些写法. 第一种使用for循环 var selectValues = { "1": "test 1", "2": "test 2" }; for (key in selectValues) { if (typeof (selectValues[k

总结jquery中对select和option的基本操作

jQuery获取Select选择的Text和Value:$("#select_id").change(function(){//code...}); //为Select添加事件,当选择其中一项时触发 var checkText=$("#select_id").find("option:selected").text(); //获取Select选择的Text var checkValue=$("#select_id").val(

select remove option safari 兼容

select 移除某一 option 的 javascript 公司用的代码是 var ddlPrimaryResource = document.getElementById(ddlPrimaryResourceID); ddlPrimaryResource.options[ddlPrimaryResource.selectedIndex].remove(); 这段代码在 chrome 里执行OK ,但是在 safari 里却报错了 TypeError: 'undefined' is not

得到select所有option里的值

得到select所有option里的值 . 2012-02-24 11:04:43|  分类: html|举报|字号 订阅 1://取得所有的option个数document.getElementById('---').options.length 2://取得每个option的ID值document.getElementById('---').options[i].value 3://取得每个option在页面显示的文本document.getElementById('---').option

JQuery中对Select的option项的添加、删除、取值

jQuery获取Select选择的Text和Value: $("#select_id").change(function(){//code...}); //为Select添加事件,当选择其中一项时触发 var checkText=$("#select_id").find("option:selected").text(); //获取Select选择的Text var checkValue=$("#select_id").val

select操作option

jQuery获取Select选择的Text和Value: 代码如下: $("#select_id").change(function(){//code...}); //为Select添加事件,当选择其中一项时触发 var checkText=$("#select_id").find("option:selected").text(); //获取Select选择的Text var checkValue=$("#select_id"