jq获取服务器端控件值小结

本文详细介绍了如何使用jQuery获取服务器端DropdownList、RadioButtonList、CheckboxList及TextBox的值,包括获取文本、值、索引等关键操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 —、获取dropdownlist的text(ddlList为服务器端dropdownlist的ID,生成name属性等于ddlList的select标签)

  $("#ddlList option:selected").text()

二、获取dropdownlist的value(ddlList为服务器端dropdownlist的ID,生成name属性等于ddlList的select标签)

  $("#ddlList").val()

三、获取radiobuttonlist的text(rbtList为服务器端radiobuttonlist的ID,生成name属性等于rbtList的一组input标签)

  $("input[name='rbtList']:checked+label").text()

四、获取radiobuttonlist的value(rbtList为服务器端radiobuttonlist的ID,生成name属性等于rbtList的一组input标签)

  $("input[name='rbtList']:checked").val()
五、获取textbox的值,$("#time").val();

 

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;
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值