教程集 www.jiaochengji.com
教程集 >  jQuery  >  jquery 教程  >  正文 JQuery中对服务器控件 DropdownList, RadioButtonList, CheckboxList的操作总结

JQuery中对服务器控件 DropdownList, RadioButtonList, CheckboxList的操作总结

发布时间:2013-06-29   编辑:jiaochengji.com
JQuery中对服务器控件 DropdownList, RadioButtonList, CheckboxList的操作总结,需要的朋友可以参考下。
jquery中对服务器控件 DropdownList, RadioButtonList, CheckboxList的操作总结,需要的朋友可以参考下。 一: DropDownList
-------------------------------------------------------------------------------------------
在使用 JQuery 进行遍历操作时,
$("input").each(function(i) {
......
}
当操作对象的类型为 dropdownlist时:(备注:在firefox下DropDownList的类型为"select-one")
获得所选中的值: $(this).val(); (如果不是遍历操作时,$(this) 就替换成 $('#控件的Id') )
获取选中的文本: $(this).find("option:selected").text(); 或者 $("#控件的name option:selected").text();
获取选中的索引: $(this).get(0).selectedIndex;
二:RadioButtonList
-------------------------------------------------------------------------------------------
如果页面只有一个RadioButtonList时,可以直接用 $("input[type='radio']:checked").val() 来获得 所选中的值
如果页面有2个或多个RadioButtonList时:
第一步: 取到RadioButtonList控件的Id,设置 var objId=控件Id;
第二步:取到控件的Name, 设置 var radioName = $("input[id^='" + objId + "']").eq(0).attr('name');
第三步:取值
  获得所选中的值: $("input[name='" + radioName + "']:checked").val());
  获得所选中的文本: $("input[name='" + radioName + "']:checked+label").text());

三:CheckBoxList
-------------------------------------------------------------------------------------------
判断是否有选中的一个方法,objId为 CheckBoxList的 Id
目前暂时无法用js直接获得服务器控件CheckBoxList的value值,只能通过一些小技巧来实现,例如添加额外的属性
代码中 selectedText 是获得 所选中的文本值,selectedValue 是获得 所选中的值
复制代码 代码如下:

function hasCheckedByCheckbox(objId) {
var checkedCount = 0;
$("input[id^='" + objId + "']").each(function() {
// var checkName = $(this).attr('name');
// var selectedText = $("input[name='" + checkName + "']:checked+label").text();
// var selectedValue = $(this).parent('span').attr('alt'); //利用hack来实现用js获取checkboxList所选中的值,需要在<asp:ListItem 里添加一个额外的属性 alt
if ($(this).attr('checked')) {
checkedCount++;
}
});
return checkedCount > 0;
}

您可能感兴趣的文章:
JQuery中对服务器控件 DropdownList, RadioButtonList, CheckboxList的操作总结
jquery获取ASP.NET服务器端控件dropdownlist和radiobuttonlist生成客户端HTML标签后的value和text值
asp.net遍历页面中所有TextBox,并赋值为String.Empty的方法
asp.net Web服务器控件教程
net入门教程:ASP.NET ArrayList 对象
Jquery获得控件值的三种方法总结
js jquery获取随机生成id的服务器控件的三种方法
Jquery 获得服务器控件值的方法小结
net 教程:ASP.NET SortedList 对象
在JQuery dialog里的服务器控件 事件失效问题

[关闭]
~ ~