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

2019-12-24,,,,

一: 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中的RadioButton,input,CheckBox取值赋值实现代码
  • asp.net使用jQuery获取RadioButtonList成员选中内容和值示例
  • 基于jquery自定义的漂亮单选按钮RadioButton
  • jquery判断RadioButtonList和RadioButton中是否有选中项示例
  • jQuery中RadioButtonList的功能及用法实例介绍
  • Jquery中的CheckBox、RadioButton、DropDownList的取值赋值实现代码
  • jquery获取ASP.NET服务器端控件dropdownlist和radiobuttonlist生成客户端HTML标签后的value和text值
  • jQuery实现 RadioButton做必选校验功能

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

下载本文的Word格式文档,以方便收藏与打印。