当前位置:首页>正文

jquery怎么设置下拉列表被选中 jquery 操作下拉框 选中某项 怎么写

2023-06-26 08:03:10 互联网 未知

jquery怎么设置下拉列表被选中

参考以下两种方法:

设置option的selected属性为true
设置select标签的value值为需要选中的值
实例演示如下:
1、根据演示需要,给出一个示例HTML结构
option-1 option-2 option-3 2、jquery代码 $(function(){ // 方法1:设置option的selected属性为true $("#btn1").click(function() { // 第一个按钮单击事件 $("select option").each(function() { // 遍历所有option,如果option内容为option-2,就设置起selected属性为true if($(this).text()=="option-2") $(this).prop("selected",true) }) }) // 方法2:设置select标签的value值为需要选中的值 $("#btn2").click(function() { // 第二个按钮的单击事件 $("select").val("3") // 设置option值为3的选项选中 }) })3、效果演示

jquery 操作下拉框 选中某项 怎么写?

如果用jquery获得下拉菜单的选择项

$("#num").change(function(){   //你下拉一下来看哈~我暂时加的是change方法~具体根据你需要~
alert("选中的value:" $(this).val() " " "选中的index:" $(this).attr("selectedIndex"))
})