jQuery获取Radio选择的Value值:
jQuery获取Radio选择的Value值:
- $("input").val(); //选择被选中Radio的Value值
- $("#text_id").focus(function(){//code...}); //事件 当对象text_id获取焦点时触发
- $("#text_id").blur(function(){//code...}); //事件 当对象text_id失去焦点时触发
- $("#text_id").select(); //使文本框的Vlaue值成选中状态
- $("input").attr("checked",true); jQuery获取CheckBox选择的Value
语法解释:
-
$("input"); //选择被选中CheckBox元素的集合 如果你想得到Value值,你需要遍历
-
$($("input")).each(function(){arrChk+=this.value + ',';}); //遍历被选中CheckBox元素的集合 得到Value值
-
$("#checkbox_id").attr("checked"); //获取一个CheckBox的状态(有没有被选中,返回true/false)
-
$("#checkbox_id").attr("checked",true); //设置一个CheckBox的状态为选中(checked=true)
-
$("#checkbox_id").attr("checked",false); //设置一个CheckBox的状态为不选中(checked=false)
-
$("input").attr("checked",$("#checkbox_id").attr("checked"));
-
$("#text_id").val().split(","); //将Text的Value值以','分隔 返回一个数组
jQuery--checkbox全选/取消全选
用JavaScript使页面上的一组checkbox全选/取消全选,逻辑很简单,实现代码也没有太难的语法。但使用jQuery实现则更简单,代码也很简洁,精辟! 1 2 3 4 全选/取消全选
jQuery.attr 获取/设置对象的属性值,如:
$("input").attr("checked"); //读取所有name为'chk_list'对象的状态(是否选中)
$("input").attr("checked",true); //设置所有name为'chk_list'对象的checked为true
再如:
$("#img_1").attr("src","test.jpg"); //设置ID为img_1的src的值为'test.jpg' $("#img_1").attr("src"); //读取ID为img_1的src值
下面的代码是获取上面实例中选中的checkbox的value值:
jQuery-对Select的操作集合
jQuery获取Select选择的Text和Value:
语法解释:
- $("#select_id").change(function(){//code...}); //为Select添加事件,当选择其中一项时触发
- var checkText=$("#select_id").find("option:selected").text(); //获取Select选择的Text
- var checkValue=$("#select_id").val(); //获取Select选择的Value
- var checkIndex=$("#select_id ").get(0).selectedIndex; //获取Select选择的索引值
- var maxIndex=$("#select_id option:last").attr("index"); //获取Select最大的索引值 jQuery设置Select选择的Text和Value: 语法解释:
- $("#select_id ").get(0).selectedIndex=1; //设置Select索引值为1的项选中
- $("#select_id ").val(4); //设置Select的Value值为4的项选中
- $("#select_id option").attr("selected", true); //设置Select的Text值为jQuery的项选中
jQuery添加/删除Select的Option项:
点击一次,Select将追加一个Option 点击将在Select第一个位置插入一个Option 点击将删除Select中索引值最大Option(最后一个)
- $("#select_id").append("Text"); //为Select追加一个Option(下拉项)
- $("#select_id").prepend("请选择"); //为Select插入一个Option(第一个位置)
- $("#select_id option:last").remove(); //删除Select中索引值最大Option(最后一个)
- $("#select_id option").remove(); //删除Select中索引值为0的Option(第一个)
- $("#select_id option").remove(); //删除Select中Value='3'的Option
- $("#select_id option").remove(); //删除Select中Text='4'的Option
JQUERY获取text,areatext,radio,checkbox,select值
jquery radio取值,checkbox取值,select取值,radio选中,checkbox选中,select选中,及其相关 获取一组radio被选中项的值 var item = $('input').val(); 获取select被选中项的文本 var item = $("select option").text(); select下拉框的第二个元素为当前选中值 $('#select_id').selectedIndex = 1; radio单选组的第二个元素为当前选中值 $('input').get(1).checked = true; 获取值: 文本框,文本区域:$("#txt").attr("value"); 多选框checkbox:$("#checkbox_id").attr("value"); 单选组radio: $("input").val(); 下拉框select: $('#sel').val(); 控制表单元素: 文本框,文本区域:$("#txt").attr("value",'');//清空内容 $("#txt").attr("value",'11');//填充内容 多选框checkbox: $("#chk1").attr("checked",'');//不打勾 $("#chk2").attr("checked",true);//打勾 if($("#chk1").attr('checked')==undefined) //判断是否已经打勾
单选组radio: $("input").attr("checked",'2');//设置value=2的项目为当前选中项 下拉框select: $("#sel").attr("value",'-sel3');//设置value=-sel3的项目为当前选中项 $("11112222").appendTo("#sel")//添加下拉框的option $("#sel").empty();//清空下拉框
//遍历option和添加、移除option function changeShipMethod(shipping){ var len = $("select option").length if(shipping.value != "CA"){ $("select option").each(function(){ if($(this).val() == 111){ $(this).remove(); } }); }else{$("UPS Ground").appendTo($("select"));}}
//取得下拉選單的選取值
$(#testSelect option:selected').text(); 或$("#testSelect").find('option:selected').text(); 或$("#testSelect").val(); ////////////////////////////////////////////////////////////////// 记性不好的可以收藏下: 1,下拉框:
var cc1 = $(".formc select option").text(); //得到下拉菜单的选中项的文本(注意中间有空格) var cc2 = $('.formc select').val(); //得到下拉菜单的选中项的值 var cc3 = $('.formc select').attr("id"); //得到下拉菜单的选中项的ID属性值 $("#select").empty();//清空下拉框//$("#select").html(''); $("1111").appendTo("#select")//添加下拉框的option
稍微解释一下: 1.select option 表示具有name 属性, 并且该属性值为'country' 的select元素 里面的具有selected 属性的option 元素; 可以看出有@开头的就表示后面跟的是属性。
2,单选框: $("input").val(); //得到单选框的选中项的值(注意中间没有空格) $("input").attr("checked",'checked'); //设置单选框value=2的为选中状态.(注意中间没有空格)
3,复选框: $("input").val(); //得到复选框的选中的第一项的值 $("input").each(function(){ //由于复选框一般选中的是多个,所以可以循环输出 alert($(this).val()); });
$("#chk1").attr("checked",'');//不打勾 $("#chk2").attr("checked",true);//打勾 if($("#chk1").attr('checked')==undefined){} //判断是否已经打勾 当然jquery的选择器是强大的. 还有很多方法.
aaass
获取一组radio被选中项的值 var item = $('input').val(); 获取select被选中项的文本 var item = $("select option").text(); select下拉框的第二个元素为当前选中值 $('#select_id').selectedIndex = 1; radio单选组的第二个元素为当前选中值 $('input').get(1).checked = true; 获取值: 文本框,文本区域:$("#txt").attr("value"); 多选框checkbox:$("#checkbox_id").attr("value"); 单选组radio: $("input").val(); 下拉框select: $('#sel').val(); 控制表单元素: 文本框,文本区域:$("#txt").attr("value",'');//清空内容 $("#txt").attr("value",'11');//填充内容 多选框checkbox: $("#chk1").attr("checked",'');//不打勾 $("#chk2").attr("checked",true);//打勾 if($("#chk1").attr('checked')==undefined) //判断是否已经打勾 单选组radio: $("input").attr("checked",'2');//设置value=2的项目为当前选中项 下拉框select: $("#sel").attr("value",'-sel3');//设置value=-sel3的项目为当前选中项 $("11112222").appendTo("#sel")//添加下拉框的option $("#sel").empty();//清空下拉框
jQuery取得select选中的值
记录一下。
本来以为jQuery("#select1").val();是取得选中的值,
那么jQuery("#select1").text();就是取得的文本。
这是不正确的,正确做法是:
jQuery("#select1 option:selected").text();
两个select之间option的互相添加操作(jquery实现)
两个select,将其中一个select选中的选项添加到另一个select中,或者点击全部添加按钮将所有的option都添加过去. 自己写了一个很简单的jquery插件,在页面中调用其中的函数就可实现. 插件源代码(listtolist.js):
Js代码
/**
fromid:源list的id.
toid:目标list的id.
moveOrAppend参数("move"或者是"append"):
move -- 源list中选中的option会删除.源list中选中的option移动到目标list中,若目标list中已存在则该option不添加.
append -- 源list中选中的option不会删除.源list中选中的option添加到目标list的后面,若目标list中已存在则该option不添加.
isAll参数(true或者false):是否全部移动或添加
*/
jQuery.listTolist = function(fromid,toid,moveOrAppend,isAll) {
if(moveOrAppend.toLowerCase() == "move") { //移动
if(isAll == true) { //全部移动
$("#"+fromid+" option").each(function() {
//将源list中的option添加到目标list,当目标list中已有该option时不做任何操作.
$(this).appendTo($("#"+toid+":not(:has(option))"));
});
$("#"+fromid).empty(); //清空源list
}
else if(isAll == false) {
$("#"+fromid+" option:selected").each(function() {
//将源list中的option添加到目标list,当目标list中已有该option时不做任何操作.
$(this).appendTo($("#"+toid+":not(:has(option))"));
//目标list中已经存在的option并没有移动,仍旧在源list中,将其清空.
if($("#"+fromid+" option").length > 0) {
$("#"+fromid).get(0)
.removeChild($("#"+fromid+" option").get(0));
}
});
}
}
else if(moveOrAppend.toLowerCase() == "append") {
if(isAll == true) {
$("#"+fromid+" option").each(function() {
$("<option></option>")
.val($(this).val())
.text($(this).text())
.appendTo($("#"+toid+":not(:has(option))"));
});
}
else if(isAll == false) {
$("#"+fromid+" option:selected").each(function() {
$("<option></option>")
.val($(this).val())
.text($(this).text())
.appendTo($("#"+toid+":not(:has(option))"));
});
}
}
};
/**
功能大体同上("move").
不同之处在于当源list中的选中option在目标list中存在时,源list中的option不会删除.
isAll参数(true或者false):是否全部移动或添加
*/
jQuery.list2list = function(fromid,toid,isAll) {
if(isAll == true) {
$("#"+fromid+" option").each(function() {
$(this).appendTo($("#"+toid+":not(:has(option))"));
});
}
else if(isAll == false) {
$("#"+fromid+" option:selected").each(function() {
$(this).appendTo($("#"+toid+":not(:has(option))"));
});
}
};
其他有关select的取值或赋值方式: 获取select被选中项的文本 var item = $("select option").text(); select下拉框的第二个元素为当前选中值 $('#stsoft').selectedIndex = 1; 获取value值 $('#stsoft').val(); 设置value=1的项目为当前选中项 $("#stsoft").attr("value",“1”); $('#stsoft').val(“1”);
Js代码
-
/** -
fromid:源list的id. -
toid:目标list的id. -
moveOrAppend参数("move"或者是"append"): -
move -- 源list中选中的option会删除.源list中选中的option移动到目标list中,若目标list中已存在则该option不添加. -
append -- 源list中选中的option不会删除.源list中选中的option添加到目标list的后面,若目标list中已存在则该option不添加. -
isAll参数(true或者false):是否全部移动或添加 -
*/ - jQuery.listTolist = function(fromid,toid,moveOrAppend,isAll) {
-
if(moveOrAppend.toLowerCase() == "move") { //移动 -
if(isAll == true) { //全部移动 -
$("#"+fromid+" option").each(function() { -
//将源list中的option添加到目标list,当目标list中已有该option时不做任何操作. -
$(this).appendTo($("#"+toid+":not(:has(option))")); -
}); -
$("#"+fromid).empty(); //清空源list -
} -
else if(isAll == false) { -
$("#"+fromid+" option:selected").each(function() { -
//将源list中的option添加到目标list,当目标list中已有该option时不做任何操作. -
$(this).appendTo($("#"+toid+":not(:has(option))")); -
//目标list中已经存在的option并没有移动,仍旧在源list中,将其清空. -
if($("#"+fromid+" option").length > 0) { -
$("#"+fromid).get(0) -
.removeChild($("#"+fromid+" option").get(0)); -
} -
}); -
} -
} -
else if(moveOrAppend.toLowerCase() == "append") { -
if(isAll == true) { -
$("#"+fromid+" option").each(function() { -
$("<option></option>") -
.val($(this).val()) -
.text($(this).text()) -
.appendTo($("#"+toid+":not(:has(option))")); -
}); -
} -
else if(isAll == false) { -
$("#"+fromid+" option:selected").each(function() { -
$("<option></option>") -
.val($(this).val()) -
.text($(this).text()) -
.appendTo($("#"+toid+":not(:has(option))")); -
}); -
} -
} - }; /**
- 功能大体同上("move").
- 不同之处在于当源list中的选中option在目标list中存在时,源list中的option不会删除.
- isAll参数(true或者false):是否全部移动或添加
- jQuery.list2list = function(fromid,toid,isAll) {
-
if(isAll == true) { -
$("#"+fromid+" option").each(function() { -
$(this).appendTo($("#"+toid+":not(:has(option))")); -
}); -
} -
else if(isAll == false) { -
$("#"+fromid+" option:selected").each(function() { -
$(this).appendTo($("#"+toid+":not(:has(option))")); -
}); -
} - };
isAllif(isAll == true) { //$("#"+fromid+" option").each(function() { //$(this).appendTo($("#"+toid+":not(:has(option))")); }); $("#"+fromid).empty(); //} else if(isAll == false) { $("#"+fromid+" option:selected").each(function() { //$(this).appendTo($("#"+toid+":not(:has(option))")); //if($("#"+fromid+" option").length > 0) { $("#"+fromid).get(0) .removeChild($("#"+fromid+" option").get(0)); } }); } } else if(moveOrAppend.toLowerCase() == "append") { if(isAll == true) { $("#"+fromid+" option").each(function() { $("") .val($(this).val()) .text($(this).text()) .appendTo($("#"+toid+":not(:has(option))")); }); } else if(isAll == false) { $("#"+fromid+" option:selected").each(function() { $("") .val($(this).val()) .text($(this).text()) .appendTo($("#"+toid+":not(:has(option))")); }); } } }; /** isAll$("#"+fromid+" option").each(function() { $(this).appendTo($("#"+toid+":not(:has(option))")); }); } else if(isAll == false) { $("#"+fromid+" option:selected").each(function() { $(this).appendTo($("#"+toid+":not(:has(option))")); }); } };