jQuery让下拉列选中

来源:互联网 发布:ecshop php 编辑:程序博客网 时间:2024/06/03 17:43

方法一:

$('#select option:eq(2)').attr('selected','selected'); 

方法二:

1、根据演示需要,给出一个示例HTML结构

<select id="test">    <option value="1">option-1</option>    <option value="2">option-2</option>    <option value="3">option-3</option></select><input type="button" id="btn1" value="设置option-2选中"><input type="button" id="btn2" value="设置value=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的选项选中    });});
原创粉丝点击