select用法

<select name="" id="select">
	<option value="01">地块一</option>
	<option value="02">地块二</option>
	<option value="03">地块三</option>
	<option value="04">地块四</option>
	<option value="05">地块五</option>
</select>  
/***如何设置默认选中的选项***/
    方法一:  标签属性:   < option value = "01" selected = "selected" > 地块一 < /option>  
	        jQuery:  $( "#select option[value='" + val + "']" ).attr( "selected", true );
		    js:   document.getElementById( "select" )[ 2 ].selected = true;                                 	        jQuery:  $( "#select option[value='" + val + "']" ).attr( "selected", true );
		    js:   document.getElementById( "select" )[ 2 ].selected = true;                                 
/***如何获得选项option的值***/
  方法一: var obj = document.getElementById( 'select' );
	  var index = obj.selectedIndex; //序号,取当前选中选项的序号
	  var val = obj.options[ index ].value;
  方法二: var val = $( '#select' ).val()
/***如何获得选项option的文本***/
         var obj = document.getElementById( 'select' );
         var index = obj.selectedIndex; //序号,取当前选中选项的序号
	 var text = obj.options[ index ].text;
/***如何动态创建select***/
    function createSelect() {
	var mySelect = document.createElement( "select" );
	mySelect.id = "mySelect";
	document.body.appendChild( mySelect );
    }
    createSelect()
/***如何添加选项option***/
    function addOption() {
	var obj = document.getElementById( 'mySelect' ); //根据id查找对象,		
	obj.add( new Option( "文本", "值" ) ); //添加一个选项		
    }
    addOption()	var obj = document.getElementById( 'mySelect' ); //根据id查找对象,		
	obj.add( new Option( "文本", "值" ) ); //添加一个选项		
    }
    addOption()
/***如何删除所有选项option***/
    function removeAll() {
	 var obj = document.getElementById( 'mySelect' );
	 obj.options.length = 0;
    }
    removeAll()
	 var obj = document.getElementById( 'mySelect' );
	 obj.options.length = 0;
    }
    removeAll()
/***如何删除一个选项option通过index***/
    function removeOne() {
	var obj = document.getElementById( 'mySelect' );
	//index,要删除选项的序号,这里取当前选中选项的序号
	var index = obj.selectedIndex;
	obj.options.remove( index );
     }
     removeOne()
	var obj = document.getElementById( 'mySelect' );
	//index,要删除选项的序号,这里取当前选中选项的序号
	var index = obj.selectedIndex;
	obj.options.remove( index );
     }
     removeOne()
/***如何修改选项option***/
    var obj = document.getElementById( 'mySelect' );
    var index = obj.selectedIndex; //序号,取当前选中选项的序号
    var val = obj.options[ index ] = new Option( "新文本", "新值" );
/***如何删除select***/
    function removeSelect() {
	 var mySelect = document.getElementById( "mySelect" );
	 mySelect.parentNode.removeChild( mySelect );
     }
   removeSelect()
	 var mySelect = document.getElementById( "mySelect" );
	 mySelect.parentNode.removeChild( mySelect );
     }
   removeSelect()

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值