<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>对下拉列表select的基本操作</title>
<script src="jquery-1.8.2.min.js"></script>
</head>

<body>
<select name="jumpMenu" id="jumpMenu" >
<option value="1">111</option>
<option value="2">222</option>
<option value="3">333</option>
<option value="4">444</option>
<option value="5">555</option>
</select>

<script type="text/javascript">
$(function(){


$("#jumpMenu").val(4);//设置value值为4的option被选中

var array = new Array();
$("#jumpMenu option").each(function(i,obj){
var value = $(this).text();

if($(this).text()==444){ //设置text值为444的option被选中
$(this).attr("selected","selected");
}
array.push(value);

})

$("#jumpMenu option[value='3']").attr("selected", "selected"); //设置value值为3的option被选中
var v1 = $("#jumpMenu").find("option:selected").text();//获取option被选中的 text
var v2 = $("#jumpMenu ").val();//获取option被选中的 value




});
</script>
</body>

radio:

1.获取radio选中值,三种方法都可以:

$('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');

4.遍历Radio

$('input:radio').each(function(index,domEle){

//写入代码

});


select:

1.获取选中项的Value值:

$('select#sel option:selected').val();

或者

$('select#sel').find('option:selected').val();

获取选中项的Text值:

$('select#sel option:selected').text();

2. 设置第一个option为选中值:

$('select#sel option:first').attr('selected','true')


checkbox:

<script>

$("document").ready(function(){

$("#btn1").click(function(){

$("[name='checkbox']").attr("checked",'true');//全选

})

$("#btn2").click(function(){

$("[name='checkbox']").removeAttr("checked");//取消全选

})

$("#btn3").click(function(){

$("[name='checkbox']:even").attr("checked",'true');//选中所有奇数

})

$("#btn4").click(function(){

$("[name='checkbox']").each(function(){//反选

if($(this).attr("checked")){

$(this).removeAttr("checked");

}

else{

$(this).attr("checked",'true');

}

})

})

$("#btn5").click(function(){//输出选中的值

var str="";

$("[name='checkbox'][checked]").each(function(){

str+=$(this).val()+"/r/n";

//alert($(this).val());

})

alert(str);

})

})


$(function(){

var array = new Array();

$("#check1").click(function(){

if($(this).attr('checked')){

$("#div1").find("input[type='checkbox']").attr('checked',true);

}else{

$("#div1").find("input[type='checkbox']").attr('checked',false);

}

});

$("#btn").click(function(){

$("#div1").find("input[type='checkbox']:checked").each(function(index,ele){

// var v = $(this).val();

var v = $(this).val();

array.push(v);

});

alert(array);

});

});

//获取checkbox的值

function chk(){

var obj=document.getElementsByName('test'); //选择所有name="'test'"的对象,返回数组

//取到对象数组后,我们来循环检测它是不是被选中

var s='';

for(var i=0; i<obj.length; i++){

if(obj[i].checked) s+=obj[i].value+','; //如果选中,将value添加到变量s中

}

//那么现在来检测s的值就知道选中的复选框的值了

alert(s==''?'你还没有选择任何内容!':s);

}

function jqchk(){ //jquery获取复选框值

var chk_value =[];

$('input[name="test"]:checked').each(function(){

chk_value.push($(this).val());

});

alert(chk_value.length==0 ?'你还没有选择任何内容!':chk_value);

}

</script>

</HEAD>

<body mce_>

<div >

<form name="form1" method="post" action="">

<input type="button" id="btn1" value="全选">

<input type="button" id="btn2" value="取消全选">

<input type="button" id="btn3" value="选中所有奇数">

<input type="button" id="btn4" value="反选">

<input type="button" id="btn5" value="获得选中的所有值">

<br /><br />

<input type="checkbox" name="checkbox" value="checkbox1">

checkbox1

<input type="checkbox" name="checkbox" value="checkbox2">

checkbox2

<input type="checkbox" name="checkbox" value="checkbox3">

checkbox3

<input type="checkbox" name="checkbox" value="checkbox4">

checkbox4

<input type="checkbox" name="checkbox" value="checkbox5">

checkbox5

<input type="checkbox" name="checkbox" value="checkbox6">

checkbox6

</form>

</div>


<div>

<input type="checkbox" id="check1" />全选<br />

<div id="div1" >

<input type="checkbox" value="apple"/>苹果<br />

<input type="checkbox" value="balana"/>香蕉<br />

<input type="checkbox" value="pear"/>梨子<br />

<input type="checkbox" value="orange"/>橘子<br />

<input type="button" id="btn" value="获取选择的值"/>

</div>

</div>

</body>

</HTML>