---
title: "JQuery操作select下拉列表框"
author: "txf2004"
date: "2009-08-04T15:23:00Z"
tags: []
slug: "jquery-select"
draft: false
lastmod: "2009-09-18T18:24:11Z"
id: 471145
url: "https://txf2004.iteye.com/blog/471145"
---

JQuery操作select标签的方法,一般如下使用:  
$("#select1")\[0].options(index).selected = true; //使第index个option选中  
$("#select1")\[0].options(3).text //取索引为3的option值

获取select被选中项的文本  
var item = $("select\[@name= stsoft] option\[@selected]").text();  
select下拉框的第二个元素为当前选中值  
$('#stsoft')\[0].selectedIndex = 1;  
获取value值  
$('#stsoft').val();  
设置value=1的项目为当前选中项  
$("#stsoft").attr("value",“1”);  
$('#stsoft').val(“1”);

$(".update").click(function()...{  
var soft = $(".SoftType").text();  
$(".SoftType").html($("#stsoft").clone());  
for(var i=0; i&lt;$(".SoftType select option").length; i++)...{  
if($(".SoftType select")\[0].options(i).text== soft)...{  
$(".SoftType select")\[0].selectedIndex = i;  
}  
}

var rowId = $(".RowId").text();  
var content='\\  
&lt;a href="javascript:void(0);" onclick="Update('+ rowId +');"&gt;更新&lt;/a&gt;&nbsp;&nbsp;\\  
&lt;a href="javascript:void(0);" onclick="Cencel('+ rowId +');"&gt;取消&lt;/a&gt;\\  
';  
$(".update").html(content);  
});

如下举一例:实现的功能是通过点击事件动态复制一个select到table的td中，并利用td包含的文本内容找到对应的select选中项，代码如下：  
&lt;!--下拉框--&gt;  
&lt;select id="stsoft" name="stsoft"&gt;  
&lt;option value="1"&gt;11&lt;/option&gt;  
&lt;option value="2"&gt;22&lt;/option&gt;  
&lt;option value="3"&gt;33&lt;/option&gt;  
&lt;option value="4"&gt;44&lt;/option&gt;  
&lt;option value="5"&gt;55&lt;/option&gt;  
&lt;option value="6"&gt;66&lt;/option&gt;  
&lt;/select&gt;

&lt;table id="datatable" border="0" cellpadding="0" cellspacing="0"&gt;  
&lt;thead&gt;  
&lt;tr align="left"&gt;  
&lt;th&gt;  
行号&lt;/th&gt;  
&lt;th&gt;  
软件类型&lt;/th&gt;  
&lt;th&gt;  
操作&lt;/th&gt;  
&lt;/tr&gt;  
&lt;/thead&gt;  
&lt;tr id="template"&gt;  
&lt;td class="RowId"&gt;  
&lt;/td&gt;  
&lt;td class="SoftType"&gt;  
&lt;/td&gt;  
&lt;td class="update"&gt;  
&lt;/td&gt;  
&lt;/tr&gt;  
&lt;/table&gt;