---
title: "jQuery获取Radio选择的Value值:"
author: "clark1231"
date: "2012-04-16T14:48:14Z"
tags: ["jQuery"]
categories: ["编程语言"]
slug: "jquery-radio-value"
draft: false
lastmod: "2012-07-05T16:25:49Z"
id: 1487549
url: "https://caok1231.iteye.com/blog/1487549"
---

jQuery获取Radio选择的Value值:

1\. $("input\[name='radio\_name']\[checked]").val();  //选择被选中Radio的Value值

例：$('input\[name=item]:checked').val()  
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全选/取消全选](http://www.cnblogs.com/bynet/archive/2009/11/13/1602491.html)

用JavaScript使页面上的一组checkbox全选/取消全选，逻辑很简单，实现代码也没有太难的语法。但使用jQuery实现则更简单，代码也很简洁，精辟！  
&lt;input type="checkbox" name="chk\_list" id="chk\_list\_1" value="1" /&gt;1&lt;br /&gt;  
&lt;input type="checkbox" name="chk\_list" id="chk\_list\_2" value="2" /&gt;2&lt;br /&gt;  
&lt;input type="checkbox" name="chk\_list" id="chk\_list\_3" value="3" /&gt;3&lt;br /&gt;  
&lt;input type="checkbox" name="chk\_list" id="chk\_list\_4" value="4" /&gt;4&lt;br /&gt;  
&lt;input type="checkbox" name="chk\_all" id="chk\_all" /&gt;全选/取消全选  
&lt;script type="text/javascript"&gt;

$("#chk\_all").click(function() {  $("input\[name='chk\_list']").attr("checked",$(this).attr("checked"));});  
&lt;/script&gt;

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的&lt;img&gt;src的值为'test.jpg'  
$("#img\_1").attr("src");     //读取ID为img\_1的&lt;img&gt;src值

 

 下面的代码是获取上面实例中选中的checkbox的value值:  
&lt;script type="text/javascript"&gt; //获取到所有name为'chk\_list'并选中的checkbox(集合)   

 var arrChk=$("input\[name='chk\_list]:checked");    //遍历得到每个checkbox的value值

    for (var i=0;i&lt;arrChk.length;i++){     alert(arrChk\[i].value); }  
&lt;/script&gt;

&lt;script type="text/javascript"&gt;

   var arrChk=$("input\[name='chk\_list']:checked"); $(arrChk).each(function() {   window.alert(this.value);  }); });&lt;/script&gt;

 

[jQuery-对Select的操作集合\[终结篇\]](http://www.cnblogs.com/bynet/archive/2009/11/16/1603769.html)

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("&lt;option value='Value'&gt;Text&lt;/option&gt;");  //为Select追加一个Option(下拉项)  
2\. $("#select\_id").prepend("&lt;option value='0'&gt;请选择&lt;/option&gt;");  //为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值](http://www.cnblogs.com/top5/archive/2009/11/12/1601543.html)

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的项目为当前选中项   
                $("&lt;option value='1'&gt;1111&lt;/option&gt;&lt;option value='2'&gt;2222&lt;/option&gt;").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{$("&lt;option value='111'&gt;UPS Ground&lt;/option&gt;").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('');  
$("&lt;option value='1'&gt;1111&lt;/option&gt;").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的选择器是强大的. 还有很多方法.

&lt;script src="jquery-1.2.1.js" type="text/javascript"&gt;&lt;/script&gt;  
&lt;script language="javascript" type="text/javascript"&gt;  
$(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");

});  
});  
&lt;/script&gt;

&lt;a href="#"&gt;aaass&lt;/a&gt;

&lt;!--下拉框--&gt;  
&lt;select id="selectTest" name="selectTest"&gt;  
&lt;option value="1"&gt;11&lt;/option&gt;&lt;option value="2"&gt;22&lt;/option&gt;&lt;option value="3"&gt;33&lt;/option&gt;  
&lt;option value="4"&gt;44&lt;/option&gt;&lt;option value="5"&gt;55&lt;/option&gt;&lt;option value="6"&gt;66&lt;/option&gt;  
&lt;/select&gt;  
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的项目为当前选中项  
$("&lt;optionvalue='1'&gt;1111&lt;/option&gt;&lt;optionvalue='2'&gt;2222&lt;/option&gt;").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的项目为当前选中项  
$("&lt;option value='1'&gt;1111&lt;/option&gt;&lt;option value='2'&gt;2222&lt;/option&gt;").appendTo("#sel")//添加下拉框的option  
$("#sel").empty()；//清空下拉框

[jQuery取得select选中的值](http://www.cnblogs.com/greatverve/archive/2010/02/03/1662565.html)

记录一下。

本来以为jQuery("#select1").val();是取得选中的值，

那么jQuery("#select1").text();就是取得的文本。

这是不正确的，正确做法是：

jQuery("#select1  option:selected").text();

 

**[两个select之间option的互相添加操作(jquery实现)](http://www.cnblogs.com/top5/archive/2009/11/12/1601675.html)**

两个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 &gt; 0) {  

                    $("#"+fromid).get(0)  

                    .removeChild($("#"+fromid+" option\[value="+$(this).val()+"]").get(0));  

                }  

            });  

        }  

    }  

    else if(moveOrAppend.toLowerCase() == "append") {  

        if(isAll == true) {  

            $("#"+fromid+" option").each(function() {  

                $("&lt;option&gt;&lt;/option&gt;")  

                .val($(this).val())  

                .text($(this).text())  

                .appendTo($("#"+toid+":not(:has(option\[value="+$(this).val()+"]))"));  

            });  

        }  

        else if(isAll == false) {  

            $("#"+fromid+" option:selected").each(function() {  

                $("&lt;option&gt;&lt;/option&gt;")  

                .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()+"]))"));  

        });  

    }  

};  

&lt;script type="text/javascript"&gt;  
   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;      
   });      
})      
&lt;/script&gt;

其他有关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”);