Jquery中的CheckBox、RadioButton、DropDownList的取值赋值实现代码

由于Jquery的版本更新很快,代码的写法也改变了许多,以下Jquery代码适query1.4版本以上
Radio 

1.获取选中值,三种方法都可以:
$(‘input:radio:checked‘).val();
$("input[type=‘radio‘]:checked").val();
$("input[name=‘rd‘]:checked").val();
2.设置第一个Radio为选中值:
$(‘input:radio:first‘).attr(‘checked‘, ‘checked‘);
或者
$(‘input:radio:first‘).attr(‘checked‘, ‘true‘);
注: attr("checked",‘checked‘)= attr("checked", ‘true‘)= attr("checked", true)
3.设置最后一个Radio为选中值:
$(‘input:radio:last‘).attr(‘checked‘, ‘checked‘);
或者
$(‘input:radio:last‘).attr(‘checked‘, ‘true‘);
4.根据索引值设置任意一个radio为选中值:
$(‘input:radio‘).eq(索引值).attr(‘checked‘, ‘true‘);索引值=0,1,2....
或者
$(‘input:radio‘).slice(1,2).attr(‘checked‘, ‘true‘);
5.根据Value值设置Radio为选中值
$("input:radio[value=‘rd2‘]").attr(‘checked‘,‘true‘);
或者
$("input[value=‘rd2‘]").attr(‘checked‘,‘true‘);
6.删除Value值为rd2的Radio
$("input:radio[value=‘rd2‘]").remove();
7.删除第几个Radio
$("input:radio").eq(索引值).remove();索引值=0,1,2....
如删除第3个Radio:$("input:radio").eq(2).remove();
8.遍历Radio
$(‘input:radio‘).each(function(index,domEle){
//写入代码
});

DropDownList

1. 获取选中项:
获取选中项的Value值:
$(‘select#sel option:selected‘).val();
或者
$(‘select#sel‘).find(‘option:selected‘).val();
获取选中项的Text值:
$(‘select#seloption:selected‘).text();
或者
$(‘select#sel‘).find(‘option:selected‘).text();
2. 获取当前选中项的索引值:
$(‘select#sel‘).get(0).selectedIndex;
3. 获取当前option的最大索引值:
$(‘select#sel option:last‘).attr("index")
4. 获取DropdownList的长度:
$(‘select#sel‘)[0].options.length;
或者
$(‘select#sel‘).get(0).options.length;
5. 设置第一个option为选中值:
$(‘select#sel option:first‘).attr(‘selected‘,‘true‘)
或者
$(‘select#sel‘)[0].selectedIndex = 0;
6. 设置最后一个option为选中值:
$(‘select#sel option:last).attr(‘selected‘,‘true‘)
7. 根据索引值设置任意一个option为选中值:
$(‘select#sel‘)[0].selectedIndex =索引值;索引值=0,1,2....
8. 设置Value=4 的option为选中值:
$(‘select#sel‘).attr(‘value‘,‘4‘);
或者
$("select#sel option[value=‘4‘]").attr(‘selected‘, ‘true‘);
9. 删除Value=3的option:
$("select#sel option[value=‘3‘]").remove();
10.删除第几个option:
$(" select#sel option ").eq(索引值).remove();索引值=0,1,2....
如删除第3个Radio:
$(" select#sel option ").eq(2).remove();
11.删除第一个option:
$(" select#sel option ").eq(0).remove();
或者
$("select#sel option:first").remove();
12. 删除最后一个option:
$("select#sel option:last").remove();
13. 删除dropdownlist:
$("select#sel").remove();
14.在select后面添加一个option:
$("select#sel").append("<option value=‘6‘>f</option>");
15. 在select前面添加一个option:
$("select#sel").prepend("<option value=‘0‘>0</option>");
16. 遍历option:
$(‘ select#sel option ‘).each(function (index, domEle) {
//写入代码
});

CheckBox

1. 获取单个checkbox选中项(三种写法):
$("input:checkbox:checked").val()
或者
$("input:[type=‘checkbox‘]:checked").val();
或者
$("input:[name=‘ck‘]:checked").val();
2. 获取多个checkbox选中项:
$(‘input:checkbox‘).each(function() {
if ($(this).attr(‘checked‘) ==true) {
alert($(this).val());
}
});
3. 设置第一个checkbox 为选中值:
$(‘input:checkbox:first‘).attr("checked",‘checked‘);
或者
$(‘input:checkbox‘).eq(0).attr("checked",‘true‘);
4. 设置最后一个checkbox为选中值:
$(‘input:radio:last‘).attr(‘checked‘, ‘checked‘);
或者
$(‘input:radio:last‘).attr(‘checked‘, ‘true‘);
5. 根据索引值设置任意一个checkbox为选中值:
$(‘input:checkbox).eq(索引值).attr(‘checked‘, ‘true‘);索引值=0,1,2....
或者
$(‘input:radio‘).slice(1,2).attr(‘checked‘, ‘true‘);
6. 选中多个checkbox:
同时选中第1个和第2个的checkbox:
$(‘input:radio‘).slice(0,2).attr(‘checked‘,‘true‘);
7. 根据Value值设置checkbox为选中值:
$("input:checkbox[value=‘1‘]").attr(‘checked‘,‘true‘);
8. 删除Value=1的checkbox:
$("input:checkbox[value=‘1‘]").remove();
9. 删除第几个checkbox:
$("input:checkbox").eq(索引值).remove();索引值=0,1,2....
如删除第3个checkbox:
$("input:checkbox").eq(2).remove();
10.遍历checkbox:
$(‘input:checkbox‘).each(function (index, domEle) {
//写入代码
});
11.全部选中
$(‘input:checkbox‘).each(function() {
$(this).attr(‘checked‘, true);
});
12.全部取消选择:
$(‘input:checkbox‘).each(function () {
$(this).attr(‘checked‘,false);
});

时间: 2024-08-25 05:42:20

Jquery中的CheckBox、RadioButton、DropDownList的取值赋值实现代码的相关文章

Jquery操作下拉框(DropDownList)实现取值赋值

Jquery操作下拉框(DropDownList)想必大家都有所接触吧,下面与大家分享下对DropDownList进行取值赋值的实现代码 1. 获取选中项: 获取选中项的Value值: $('select#sel option:selected').val(); 或者 $('select#sel').find('option:selected').val(); 获取选中项的Text值: $('select#seloption:selected').text(); 或者 $('select#sel

Jquery操作复选框(CheckBox)的取值赋值实现代码

赋值 复选框 CheckBox 遍历 取值 1. 获取单个checkbox选中项(三种写法): $("input:checkbox:checked").val() 或者 $("input:[type='checkbox']:checked").val(); 或者 $("input:[name='ck']:checked").val(); 复制代码 2. 获取多个checkbox选中项: $('input:checkbox').each(funct

Jquery 中的CheckBox、 RadioButton、 DropDownList的取值赋值

1.获取选中值,三种方法都可以: $('input:radio:checked').val(): $("input[type='radio']:checked").val(); $("input[name='rd']:checked").val(); 2.设置第一个Radio为选中值: $('input:radio:first').attr('checked', 'checked'); 或者 $('input:radio:first').attr('checked'

jQuery EasyUI DataGrid Checkbox 数据设定与取值

$('#dg').datagrid({ title: 'CheckBox Selection on DataGrid', url: 'datagrid_data3.json', width: '700', rownumbers: true, columns:[[ { field:'ck',checkbox:true }, { field: 'productid', title: 'productid' }, { field: 'productname', title: 'productname'

jquery操作select取值赋值与设置选中

本节内容:jquery实现select下拉框的取值与赋值,设置选中的方法大全. 比如<select class="selector"></select> 1.设置value为pxx的项选中 复制代码代码示例: $(".selector").val("pxx"); 2.设置text为pxx的项选中 复制代码代码示例: $(".selector").find("option[text='pxx']

使用mybatis如果类属性名和数据库中的属性名不一样取值就会为null

使用mybatis时如果类属性名和数据库中的属性名不一样取值就会为null 这是不能再去改变javabean中的属性,只能改变sql语句.语句如下所示: <select id="selectEmp" resultType="com.atguigu.mybatis.bean.Employee"> select id,last_name lastName,gender,email from tbl_employee where id= #{id} </

php 练习题-session与 cookie的 取值赋值

SESSION 的取值赋值时注意无论取值赋值页面中php代码的第一行必须要写session_start(); 赋值页面: <body> <?php session_start();//一定记住用session时 每个页面都要加无论取值还是赋值页面在开头都要加上 :session_start(); //会话控制 //HTTP协议 在tcp协议基础上 //HTTP 协议称无状态协议 //SESSION的特点:1存储在服务器:2每个使用者都会生成一个session:3有默认的过期时间大概是15

[King.yue]Ext.JS 弹出窗体取值赋值

//从Grid取值var name = Ext.getCmp(gridGridID).getView().getSelectionModel().getSelection()[0].data.Name; var code = Ext.getCmp(gridGridID).getView().getSelectionModel().getSelection()[0].data.Code; //赋值ID Ext.getCmp('@V.ID_TXT_NAME').setValue(name);//不用

winform 批量控件取值赋值

以前写winform 最多写几个文本框,最近需要入录一个人员信息,那好几十个字段,一下子干蒙了,这要是一个个取值赋值都写到明天了,于是就自己写了个方法,也不是什么高大上的,就是很简单很普通很low的方法. 废话少说上代码,注意,这块我就用了个文本框,你也可以找到所有控件,尽量控件name与实体字段一样. public Dictionary<string, object> GetRS_InfoVue() { var dic = new Dictionary<string, object&g