代码:var tds = $(elem).parent().parent().find("td"); //抓取GridView上的td
$("#IDD").find("option:selected").text(tds.eq(0).html());控件:<asp:DropDownList ID="IDD" runat="server">
</asp:DropDownList>DropDownList下拉值为 ID NAME
1 AA
2 BB如果按第一句来写的话,就是给DropDownList加了一项,DropDownList选的值始终是1。
怎么写可以根据文本获取到ID??
$("#IDD").find("option:selected").text(tds.eq(0).html());控件:<asp:DropDownList ID="IDD" runat="server">
</asp:DropDownList>DropDownList下拉值为 ID NAME
1 AA
2 BB如果按第一句来写的话,就是给DropDownList加了一项,DropDownList选的值始终是1。
怎么写可以根据文本获取到ID??
var count = $(elem).find("option").length;
for (var i = 0; i < count; i++) {
if ($(elem).get(0).options[i].text == text) {
$(elem).get(0).options[i].selected = true;
break;
}
}
}
ID NAME
1 AA
2 BB
lz的意思是说从其他地方获取文本,然后在根据获取到的文本去获取DropDownList相应Name的ID值?
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Value="1">AA</asp:ListItem>
<asp:ListItem Value="2">BB</asp:ListItem>
</asp:DropDownList>
<input id="Button1" type="button" value="button" />
<script type="text/javascript">
var txt = "BB";
$("#Button1").click(function(){
alert($("#DropDownList1 option[text='"+txt+"']").attr("Value"));
})
</script>