- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">??
- <html xmlns="http://www.w3.org/1999/xhtml">??
- <head>??
- <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />??
- <title>louis-blog >> jQuery 瀵?heckbox????浣?</title>??
- <script type='text/javascript' src="jquery.js"></script>??
- <SCRIPT LANGUAGE="JavaScript">??
- <!--??
- $("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);??
- })??
- })??
- -->??
- </SCRIPT>??
- </HEAD>??
- <body style="text-align:center;margin: 0 auto;font-size: 12px;">??
- <div style="border: 1px solid #999; width: 500px; padding: 15px; background: #eee; margin-top: 150px;">??
- <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">??
- checkbox1val??
- <input type="checkbox" name="checkbox" value="checkbox2">??
- checkbox2val?
- <input type="checkbox" name="checkbox" value="checkbox3">??
- checkbox3val?
- <input type="checkbox" name="checkbox" value="checkbox4">??
- checkbox4val??
- <input type="checkbox" name="checkbox" value="checkbox5">??
- checkbox5val??
- <input type="checkbox" name="checkbox" value="checkbox6">??
- checkbox6val??
- </form>??
- </div>??
- </body>??
- </HTML>?
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
绋?寰?瑙i??涓?涓?:
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')==true){} //?ゆ??????凡缁?????
褰???query?????╁?ㄦ??寮哄ぇ??. 杩???寰?澶??規?.
???????宸變???杩?涓?涓?jquery??澶???妗?灏???浠??垮????浠ュ????????.
http://www.cssrain.cn/demo/jquery-check/checkbox.html
<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" target="_blank" rel="external nofollow" ;
?????? //alert($("#selectTest").val());
?????? alert($("#selectTest option[@selected]").text());
?????? $("#selectTest").attr("value", "2");
});
});
</script>
<a href="#" target="_blank" rel="external nofollow" >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();
澶?缁?????????锛?$("input[@typ=radio][@name=''][@checked]")
涓???妗?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()锛?//娓?绌轟???妗?
杞?杞戒?:https://www.cnblogs.com/YuanShuai/archive/2012/02/27/2370044.html