jquery 设置select选中第一个

2024-10-12 07:09:39

1、打开前端编辑软件,新建一个html文件。如图:

jquery 设置select选中第一个

3、添加点击事件。1.引入jquery库;2.新建一个<script>标签,在这个标签里面创建点击事件。代码<script>function clearSelect(){$("#test select").each(function(){$(this).find("option").eq(0).prop("selected",true)})}</script>

jquery 设置select选中第一个jquery 设置select选中第一个

5、所有代码,可以直接把所有代码复制到新建的html文件上,添加query库保存后运行即可看到效果。所有代码:<!DOCTYPE html>媪青怍牙<html><head><meta charset="UTF-8"><title>select设置默认选中第一个</title><script>function clearSelect(){$("#test select").each(function(){$(this).find("option").eq(0).prop("selected",true)})}</script></head><body><input type="button" value="清空" onclick="clearSelect();"/><div id="test"><select name=""><option value="0">请选择下拉菜单</option><option value="1">选择1</option><option value="2">选择2</option><option value="3">选择3</option></select></div></body></html>

猜你喜欢