手机
当前位置:查字典教程网 >编程开发 >Javascript教程 >JQuery中对服务器控件 DropdownList, RadioButtonList, CheckboxList的操作总结
JQuery中对服务器控件 DropdownList, RadioButtonList, CheckboxList的操作总结
摘要:一:DropDownList--------------------------------------------------------...

一: 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的操作总结】相关文章:

浅析JavaScript中的事件机制

jQuery中 prop() attr()使用详解

Node.js本地文件操作之文件拷贝与目录遍历的方法

CSS+JS构建的图片查看器

在JavaScript中使用开平方根的sqrt()方法

JS实现跳转代码:多域名指向同一空间

jQuery替换textarea中换行的方法

JS或jQuery获取ASP.NET服务器控件ID的方法

javascript用函数实现对象的方法

用于table内容排序

精品推荐
分类导航