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值以','分隔 返回一個數組
用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選擇的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 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("#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代碼
1. /**
2. fromid:源list的id.
3. toid:目標list的id.
4. moveOrAppend參數("move"或者是"append"):
5. move -- 源list中選中的option會刪除.源list中選中的option移動到目標list中,若目標list中已存在則該option不添加.
6. append -- 源list中選中的option不會刪除.源list中選中的option添加到目標list的後面,若目標list中已存在則該option不添加.
7.
8. isAll參數(true或者false):是否全部移動或添加
9. */
10. jQuery.listTolist = function(fromid,toid,moveOrAppend,isAll) {
11. if(moveOrAppend.toLowerCase() == "move") { //移動
12. if(isAll == true) { //全部移動
13. $("#"+fromid+" option").each(function() {
14. //將源list中的option添加到目標list,當目標list中已有該option時不做任何操作.
15. $(this).appendTo($("#"+toid+":not(:has(option[value="+$(this).val()+"]))"));
16. });
17. $("#"+fromid).empty(); //清空源list
18. }
19. else if(isAll == false) {
20. $("#"+fromid+" option:selected").each(function() {
21. //將源list中的option添加到目標list,當目標list中已有該option時不做任何操作.
22. $(this).appendTo($("#"+toid+":not(:has(option[value="+$(this).val()+"]))"));
23. //目標list中已經存在的option並沒有移動,仍舊在源list中,將其清空.
24. if($("#"+fromid+" option[value="+$(this).val()+"]").length > 0) {
25. $("#"+fromid).get(0)
26. .removeChild($("#"+fromid+" option[value="+$(this).val()+"]").get(0));
27. }
28. });
29. }
30. }
31. else if(moveOrAppend.toLowerCase() == "append") {
32. if(isAll == true) {
33. $("#"+fromid+" option").each(function() {
34. $("<option></option>")
35. .val($(this).val())
36. .text($(this).text())
37. .appendTo($("#"+toid+":not(:has(option[value="+$(this).val()+"]))"));
38. });
39. }
40. else if(isAll == false) {
41. $("#"+fromid+" option:selected").each(function() {
42. $("<option></option>")
43. .val($(this).val())
44. .text($(this).text())
45. .appendTo($("#"+toid+":not(:has(option[value="+$(this).val()+"]))"));
46. });
47. }
48. }
49. }; /**
50. 功能大體同上("move").
51. 不同之處在於當源list中的選中option在目標list中存在時,源list中的option不會刪除.
52. isAll參數(true或者false):是否全部移動或添加
53. jQuery.list2list = function(fromid,toid,isAll) {
54. if(isAll == true) {
55. $("#"+fromid+" option").each(function() {
56. $(this).appendTo($("#"+toid+":not(:has(option[value="+$(this).val()+"]))"));
57. });
58. }
59. else if(isAll == false) {
60. $("#"+fromid+" option:selected").each(function() {
61. $(this).appendTo($("#"+toid+":not(:has(option[value="+$(this).val()+"]))"));
62. });
63. }
64. };
isAllif(isAll == true) { //$("#"+fromid+" option").each(function() { //$(this).appendTo($("#"+toid+":not(:has(option[value="+$(this).val()+"]))")); }); $("#"+fromid).empty(); //} else if(isAll == false) { $("#"+fromid+" option:selected").each(function() { //$(this).appendTo($("#"+toid+":not(:has(option[value="+$(this).val()+"]))")); //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()+"]))")); }); } } }; /** isAll$("#"+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()+"]))")); }); } };