jquery select demo

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>test</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js" type="text/javascript"></script>    
<script>
$(document).ready(function(){
$("input[name=radioBtn1]").click(function(){
var val = $("input[name=gender][checked]").val();
alert(val);
});
$("input[name=radioBtn2]").click(function(){
$('input[name=gender]').attr("checked",'2');
//$('input[name=gender]').get(2).checked=true; //这种写法也可以
});

$("input[name=btn3]").click(function(){
$("input[name=fruit][checked]").each(function(){
alert($(this).attr("value"));
});
});

$("input[name=btn4]").click(function(){
$("input[name=fruit]").get(1).checked = true;
});

$("input[name=btn5]").click(function(){
$("input[name='fruit']").attr("checked",true);
/*
            $("input[name='fruit']").each(function(){
                $(this).attr("checked",true);
            });*/

});

$("input[name=btn6]").click(function(){
$("input[name='fruit']").attr("checked",'');
/*
            $("input[name='fruit']").each(function(){
                $(this).attr("checked",'');
            });*/
});

$("input[name=btn7]").click(function(){
var val = $("select[name=hometown]").val();
alert(val);
});

$("input[name=btn8]").click(function(){
//var val = $("select[name=hometown]").find("option:selected").text();
//var val = $("select[name=hometown] option[selected]").text();
var val = $("select[name=hometown] option:selected").text();
alert(val);
});

$("input[name=btn9]").click(function(){
//$("select[name=hometown]").get(0).selectedIndex = 1; //same

$("select[name=hometown]")[0].selectedIndex = 1;
});

$("input[name=btn10]").click(function(){
$("select[name=hometown]").empty();
});
});
</script>
</head>
<body>
        Are you male or femal? <br>
<input type="radio" name="gender" checked value="male">male<br>
<input type="radio" name="gender" value="female">female<br>
<input type="radio" name="gender" value="gay">gay<br>
<input type="button" value="获得radio选中的值" name="radioBtn1">&nbsp;&nbsp;<input type="button" value="设gay为被选项" name="radioBtn2">
<hr>
        Which fruit do you like?<br>
<input type="checkbox" name="fruit" value="apple">apple<br>
<input type="checkbox" name="fruit" value="strawberry">strawberry<br>
<input type="checkbox" name="fruit" value="mango">mango<br>
<input type="checkbox" name="fruit" value="cherry">cherry<br>
<input type="checkbox" name="fruit" value="leechee">leechee<br>
<input type="button" value="获取checkbox的选中值" name="btn3">&nbsp;&nbsp;<input type="button" value="love strawberry" name="btn4"><br><br>
<input type="button" value="全选" name="btn5">&nbsp;&nbsp;<input type="button" value="取消全选" name="btn6">
<hr>
        where is your hometown?
<select name='hometown'>
<option value='shandong'>山东</option>
<option value='sichuang'>四川</option>
<option value='shanghai'>上海</option>
</select>
<br><br>
<input type='button' name='btn7' value='select被选中的value值'>&nbsp;&nbsp;
<input type='button' name='btn8' value='select被选中的文本值'>&nbsp;&nbsp;
<input type='button' name='btn9' value='设置‘四川’为当前选中值'>&nbsp;&nbsp;
<input type='button' name='btn10' value='清空下拉框'>

</body>
</html>

你可能感兴趣的:(html,jquery,apple)