您的位置:首页 > Web前端 > JQuery

JQuery操作select下拉列表框

2009-08-04 15:23 615 查看
<script type="text/javascript"><!--
google_ad_client = "pub-4490194096475053";
/* 内容页,300x250,第一屏 */
google_ad_slot = "3685991503";
google_ad_width = 300;
google_ad_height = 250;
// --></script>
<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>

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<$(".SoftType select option").length; i++)...{
if($(".SoftType select")[0].options(i).text== soft)...{
$(".SoftType select")[0].selectedIndex = i;
}
}

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

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

<table id="datatable" border="0" cellpadding="0" cellspacing="0">
<thead>
<tr align="left">
<th>
行号</th>
<th>
软件类型</th>
<th>
操作</th>
</tr>
</thead>
<tr id="template">
<td class="RowId">
</td>
<td class="SoftType">
</td>
<td class="update">
</td>
</tr>
</table>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息