jq实现下拉框的change事件获取选中的值

jq获取选中的value值

 <select name="" id="sel">
		<option value="" selected hidden>请选择输入方式</option>
		<option value="电话">电话</option>
		<option value="微信">微信</option>
		<option value="QQ">QQ</option>
		<option value="QQ1">QQ1</option>
</select>
<script>
	$('#sel').change(function () {
     
     
			//获取选中下拉框的属性值
			let val = $('#sel option:selected').val();   //或者let val = $(this).val()
			console.log(val)
	})

</script>

补充:获取下拉框选中的索引

1.jq 获取

获取索引的如下三种方法
在这里插入图片描述

<script>
$('#sel').change(function () {
     
     
			//获取索引的如下三种方法
			let index = $('#sel').prop('selectedIndex');  
			//let index = $('option:selected', '#sel').index();
			//let index = $('#sel option').index($('#sel option:selected'))
			console.log(index)
		})
</script>

2.js 获取

<script>
	var sel=document.getElementById("sel");
	sel.onchange=function(){
     
     
	console.log(sel.selectedIndex)
	console.log(sel.options[sel.selectedIndex].value);
 } 
</script>

猜你喜欢

转载自blog.csdn.net/qq_45695853/article/details/115233376
今日推荐