You are here
jQuery获取radio值,jquery获取表单元素值
星期四, 2014-12-25 15:51 — shiping1
- jQuery获取Radio选择的Value值:
-
- 1. $("input[name='radio_name'][checked]").val(); //选择被选中Radio的Value值
- 2. $("#text_id").focus(function(){//code...}); //事件 当对象text_id获取焦点时触发
- 3. $("#text_id").blur(function(){//code...}); //事件 当对象text_id失去焦点时触发
- 4. $("#text_id").select(); //使文本框的Vlaue值成选中状态
- 5. $("input[name='radio_name'][value='要中Radio的Value值']").attr("checked",true); jQuery获取CheckBox选择的Value
-
- 语法解释:
- 1. $("input[name='checkbox_name'][checked]"); //选择被选中CheckBox元素的集合 如果你想得到Value值,你需要遍历
- 2. $($("input[name='checkbox_name'][checked]")).each(function(){arrChk+=this.value + ',';}); //遍历被选中CheckBox元素的集合 得到Value值
- 3. $("#checkbox_id").attr("checked"); //获取一个CheckBox的状态(有没有被选中,返回true/false)
- 4. $("#checkbox_id").attr("checked",true); //设置一个CheckBox的状态为选中(checked=true)
- 5. $("#checkbox_id").attr("checked",false); //设置一个CheckBox的状态为不选中(checked=false)
- 6. $("input[name='checkbox_name']").attr("checked",$("#checkbox_id").attr("checked"));
-
- 7. $("#text_id").val().split(","); //将Text的Value值以','分隔 返回一个数组
-
-
-
- jQuery--checkbox全选/取消全选
-
- 用JavaScript使页面上的一组checkbox全选/取消全选,逻辑很简单,实现代码也没有太难的语法。但使用jQuery实现则更简单,代码也很简洁,精辟!
- <input type="checkbox" name="chk_list" id="chk_list_1" value="1" />1<br />
- <input type="checkbox" name="chk_list" id="chk_list_2" value="2" />2<br />
- <input type="checkbox" name="chk_list" id="chk_list_3" value="3" />3<br />
- <input type="checkbox" name="chk_list" id="chk_list_4" value="4" />4<br />
- <input type="checkbox" name="chk_all" id="chk_all" />全选/取消全选
- <script type="text/javascript">
-
- $("#chk_all").click(function() { $("input[name='chk_list']").attr("checked",$(this).attr("checked"));});
- </script>
-
-
- jQuery.attr 获取/设置对象的属性值,如:
-
- $("input[name='chk_list']").attr("checked"); //读取所有name为'chk_list'对象的状态(是否选中)
-
- $("input[name='chk_list']").attr("checked",true); //设置所有name为'chk_list'对象的checked为true
-
- 再如:
-
- $("#img_1").attr("src","test.jpg"); //设置ID为img_1的<img>src的值为'test.jpg'
- $("#img_1").attr("src"); //读取ID为img_1的<img>src值
-
-
-
- 下面的代码是获取上面实例中选中的checkbox的value值:
- <script type="text/javascript"> //获取到所有name为'chk_list'并选中的checkbox(集合)
-
- var arrChk=$("input[name='chk_list]:checked"); //遍历得到每个checkbox的value值
-
- for (var i=0;i<arrChk.length;i++){ alert(arrChk[i].value); }
- </script>
-
- <script type="text/javascript">
-
- var arrChk=$("input[name='chk_list']:checked"); $(arrChk).each(function() { window.alert(this.value); }); });</script>
-
-
-
- jQuery-对Select的操作集合[终结篇]
-
- jQuery获取Select选择的Text和Value:
-
- 语法解释:
- 1. $("#select_id").change(function(){//code...}); //为Select添加事件,当选择其中一项时触发
- 2. var checkText=$("#select_id").find("option:selected").text(); //获取Select选择的Text
- 3. var checkValue=$("#select_id").val(); //获取Select选择的Value
- 4. var checkIndex=$("#select_id ").get(0).selectedIndex; //获取Select选择的索引值
- 5. var maxIndex=$("#select_id option:last").attr("index"); //获取Select最大的索引值
- jQuery设置Select选择的Text和Value:
- 语法解释:
- 1. $("#select_id ").get(0).selectedIndex=1; //设置Select索引值为1的项选中
- 2. $("#select_id ").val(4); //设置Select的Value值为4的项选中
- 3. $("#select_id option[text='jQuery']").attr("selected", true); //设置Select的Text值为jQuery的项选中
-
- jQuery添加/删除Select的Option项:
-
- 点击一次,Select将追加一个Option
- 点击将在Select第一个位置插入一个Option
- 点击将删除Select中索引值最大Option(最后一个)
- 1. $("#select_id").append("<option value='Value'>Text</option>"); //为Select追加一个Option(下拉项)
- 2. $("#select_id").prepend("<option value='0'>请选择</option>"); //为Select插入一个Option(第一个位置)
- 3. $("#select_id option:last").remove(); //删除Select中索引值最大Option(最后一个)
- 4. $("#select_id option[index='0']").remove(); //删除Select中索引值为0的Option(第一个)
- 5. $("#select_id option[value='3']").remove(); //删除Select中Value='3'的Option
- 5. $("#select_id option[text='4']").remove(); //删除Select中Text='4'的Option
-
-
-
- JQUERY获取text,areatext,radio,checkbox,select值
-
-
-
- jquery radio取值,checkbox取值,select取值,radio选中,checkbox选中,select选中,及其相关
- 获取一组radio被选中项的值
- var item = $('input[@name=items][@checked]').val();
- 获取select被选中项的文本
- var item = $("select[@name=items] option[@selected]").text();
- select下拉框的第二个元素为当前选中值
- $('#select_id')[0].selectedIndex = 1;
- radio单选组的第二个元素为当前选中值
- $('input[@name=items]').get(1).checked = true;
- 获取值:
- 文本框,文本区域:$("#txt").attr("value");
- 多选框checkbox:$("#checkbox_id").attr("value");
- 单选组radio: $("input[@type=radio][@checked]").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[@type=radio]").attr("checked",'2');//设置value=2的项目为当前选中项
- 下拉框select: $("#sel").attr("value",'-sel3');//设置value=-sel3的项目为当前选中项
- $("<option value='1'>1111</option><option value='2'>2222</option>").appendTo("#sel")//添加下拉框的option
- $("#sel").empty();//清空下拉框
-
- ----------------------------------------------
-
- //遍历option和添加、移除option
- function changeShipMethod(shipping){
- var len = $("select[@name=ISHIPTYPE] option").length
- if(shipping.value != "CA"){
- $("select[@name=ISHIPTYPE] option").each(function(){
- if($(this).val() == 111){
- $(this).remove();
- }
- });
- }else{$("<option value='111'>UPS Ground</option>").appendTo($("select[@name=ISHIPTYPE]"));}}
-
-
- //取得下拉選單的選取值
-
- $(#testSelect option:selected').text();
- 或$("#testSelect").find('option:selected').text();
- 或$("#testSelect").val();
- ************************************************************
- 记性不好的可以收藏下:
- 1,下拉框:
-
- var cc1 = $(".formc select[@name='country'] option[@selected]").text(); //得到下拉菜单的选中项的文本(注意中间有空格)
- var cc2 = $('.formc select[@name="country"]').val(); //得到下拉菜单的选中项的值
- var cc3 = $('.formc select[@name="country"]').attr("id"); //得到下拉菜单的选中项的ID属性值
- $("#select").empty();//清空下拉框//$("#select").html('');
- $("<option value='1'>1111</option>").appendTo("#select")//添加下拉框的option
-
- 稍微解释一下:
- 1.select[@name='country'] option[@selected] 表示具有name 属性,
- 并且该属性值为'country' 的select元素 里面的具有selected 属性的option 元素;
- 可以看出有@开头的就表示后面跟的是属性。
-
- 2,单选框:
- $("input[@type=radio][@checked]").val(); //得到单选框的选中项的值(注意中间没有空格)
- $("input[@type=radio][@value=2]").attr("checked",'checked'); //设置单选框value=2的为选中状态.(注意中间没有空格)
-
- 3,复选框:
- $("input[@type=checkbox][@checked]").val(); //得到复选框的选中的第一项的值
- $("input[@type=checkbox][@checked]").each(function(){ //由于复选框一般选中的是多个,所以可以循环输出
- alert($(this).val());
- });
-
- $("#chk1").attr("checked",'');//不打勾
- $("#chk2").attr("checked",true);//打勾
- if($("#chk1").attr('checked')==undefined){} //判断是否已经打勾
- 当然jquery的选择器是强大的. 还有很多方法.
-
- <script src="jquery-1.2.1.js" type="text/javascript"></script>
- <script language="javascript" type="text/javascript">
- $(document).ready(function(){
- $("#selectTest").change(function()
- {//alert("Hello");//alert($("#selectTest").attr("name"));//$("a").attr("href","xx.html");//window.location.href="xx.html";
- //alert($("#selectTest").val());
- alert($("#selectTest option[@selected]").text());
- $("#selectTest").attr("value", "2");
-
- });
- });
- </script>
-
-
- <a href="#">aaass</a>
-
- <!--下拉框-->
- <select id="selectTest" name="selectTest">
- <option value="1">11</option><option value="2">22</option><option value="3">33</option>
- <option value="4">44</option><option value="5">55</option><option value="6">66</option>
- </select>
- jquery radio取值,checkbox取值,select取值,radio选中,checkbox选中,select选中,及其相关获取一组radio被选中项的值
- var item = $('input[@name=items][@checked]').val();
- 获取select被选中项的文本
- var item = $("select[@name=items] option[@selected]").text();
- select下拉框的第二个元素为当前选中值
- $('#select_id')[0].selectedIndex = 1;
- radio单选组的第二个元素为当前选中值
- $('input[@name=items]').get(1).checked = true;
- 获取值:
- 文本框,文本区域:$("#txt").attr("value");
- 多选框checkbox:$("#checkbox_id").attr("value");
- 单选组radio: $("input[@type=radio][@checked]").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[@type=radio]").attr("checked",'2');//设置value=2的项目为当前选中项
- 下拉框select: $("#sel").attr("value",'-sel3');//设置value=-sel3的项目为当前选中项
- $("<optionvalue='1'>1111</option><optionvalue='2'>2222</option>").appendTo("#sel")//添加下拉框的option
- $("#sel").empty();//清空下拉框
-
- 获取一组radio被选中项的值
- var item = $('input[@name=items][@checked]').val();
- 获取select被选中项的文本
- var item = $("select[@name=items] option[@selected]").text();
- select下拉框的第二个元素为当前选中值
- $('#select_id')[0].selectedIndex = 1;
- radio单选组的第二个元素为当前选中值
- $('input[@name=items]').get(1).checked = true;
- 获取值:
- 文本框,文本区域:$("#txt").attr("value");
- 多选框checkbox:$("#checkbox_id").attr("value");
- 单选组radio: $("input[@type=radio][@checked]").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[@type=radio]").attr("checked",'2');//设置value=2的项目为当前选中项
- 下拉框select: $("#sel").attr("value",'-sel3');//设置value=-sel3的项目为当前选中项
- $("<option value='1'>1111</option><option value='2'>2222</option>").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[value="+$(this).val()+"]))"));
-
- });
-
- $("#"+fromid).empty(); //清空源list
-
- }
-
- else if(isAll == false) {
-
- $("#"+fromid+" option:selected").each(function() {
-
- //将源list中的option添加到目标list,当目标list中已有该option时不做任何操作.
-
- $(this).appendTo($("#"+toid+":not(:has(option[value="+$(this).val()+"]))"));
-
- //目标list中已经存在的option并没有移动,仍旧在源list中,将其清空.
-
- if($("#"+fromid+" option[value="+$(this).val()+"]").length > 0) {
-
- $("#"+fromid).get(0)
-
- .removeChild($("#"+fromid+" option[value="+$(this).val()+"]").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[value="+$(this).val()+"]))"));
-
- });
-
- }
-
- else if(isAll == false) {
-
- $("#"+fromid+" option:selected").each(function() {
-
- $("<option></option>")
-
- .val($(this).val())
-
- .text($(this).text())
-
- .appendTo($("#"+toid+":not(:has(option[value="+$(this).val()+"]))"));
-
- });
-
- }
-
- }
-
- };
-
- /**
-
- 功能大体同上("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[value="+$(this).val()+"]))"));
-
- });
-
- }
-
- else if(isAll == false) {
-
- $("#"+fromid+" option:selected").each(function() {
-
- $(this).appendTo($("#"+toid+":not(:has(option[value="+$(this).val()+"]))"));
-
- });
-
- }
-
- };
-
- <script type="text/javascript">
- jQuery(function($)
- { //获取select文本和值
- $("#submitBut").click(function(){ //注意空格
- //var strText = $("select[@name=fselect] option[@selected]").text();
- // var strValue = $("select[@name=fselect] option[@selected]").val();
- //alert(strText + ":" + strValue);
- //选中值为t3项
- $("#fselect").attr("value", "t3"); //选中第二项
- $('#fselect')[0].selectedIndex = 1;
- alert($("#fselect")[0].length);
- });
- //select改变时获取当前选项的值和文本
- $("#fselect").change(function(){ //获取总的选项
- //alert($(this)[0].length);
- //获取的所有的文本 var strText = $(this).text();
- //获取当前选中值 var strValue = $(this).val();
- //alert(strText + ":" + strValue);
- //选中值为t3项 //选中第二项
- //$(this)[0].selectedIndex = 3; //$(this).attr("value", "t3"); / /$("#fselect")[0].options[2].selected = true;
- //获得当前选中的文本
- //显示索引为2的文本
- var nCurrent = $(this)[0].selectedIndex;
- alert($("#fselect")[0].options[nCurrent].text);
- alert(strValue);
- });
-
- //增加select
- $("#add").click(function(){
- var nLength = $("#fselect")[0].length;
- var option = document.createElement("option");;
- option.text = "Text" + (nLength+1).toString();
- option.value = "t" + (nLength+1).toString();
- $("#fselect")[0].options.add(option);
- //$("#fselect").addOption("Text" + (nLength+1).toString(), "t" + (nLength+1).toString(), true);
- }); //清空select
- $("#clear").click(function(){
- $("#fselect").empty();
- }); //清空一项
- $("#remove").click(function(){
- var index = $("#fselect")[0].selectedIndex;
- //$("#fselect")[0].remove(index);
- $("#fselect")[0].options[index] = null;
- });
- })
- </script>
-
- 其他有关select的取值或赋值方式:
- 获取select被选中项的文本
- var item = $("select[@name= stsoft] option[@selected]").text();
- select下拉框的第二个元素为当前选中值
- $('#stsoft')[0].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[value="+$(this).val()+"]))"));
- });
- $("#"+fromid).empty(); //清空源list
- }
- else if(isAll == false) {
- $("#"+fromid+" option:selected").each(function() {
- //将源list中的option添加到目标list,当目标list中已有该option时不做任何操作.
- $(this).appendTo($("#"+toid+":not(:has(option[value="+$(this).val()+"]))"));
- //目标list中已经存在的option并没有移动,仍旧在源list中,将其清空.
- if($("#"+fromid+" option[value="+$(this).val()+"]").length > {
- $("#"+fromid).get(
- .removeChild($("#"+fromid+" option[value="+$(this).val()+"]").get();
- }
- });
- }
- }
- 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[value="+$(this).val()+"]))"));
- });
- }
- else if(isAll == false) {
- $("#"+fromid+" option:selected").each(function() {
- $("<option></option>")
- .val($(this).val())
- .text($(this).text())
- .appendTo($("#"+toid+":not(:has(option[value="+$(this).val()+"]))"));
- });
- }
- }
- }; /**
- 功能大体同上("move").
- 不同之处在于当源list中的选中option在目标list中存在时,源list中的option不会删除.
- isAll参数(true或者false):是否全部移动或添加
- jQuery.listist = function(fromid,toid,isAll) {
- if(isAll == true) {
- $("#"+fromid+" option").each(function() {
- $(this).appendTo($("#"+toid+":not(:has(option[value="+$(this).val()+"]))"));
- });
- }
- else if(isAll == false) {
- $("#"+fromid+" option:selected").each(function() {
- $(this).appendTo($("#"+toid+":not(:has(option[value="+$(this).val()+"]))"));
- });
- }
- };
- 引自:http://www.cnblogs.com/tangself/archive/2010/04/14/1711684.html