jquery select demo

本文介绍了一个使用jQuery操作DOM元素的示例,包括读取和设置单选按钮与复选框的状态,选择并显示下拉菜单的选定值。通过具体代码展示了如何通过jQuery简化常见的DOM操作任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


<!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">  <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">  <input type="button" value="love strawberry" name="btn4"><br><br>
<input type="button" value="全选" name="btn5">  <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值'>  
<input type='button' name='btn8' value='select被选中的文本值'>  
<input type='button' name='btn9' value='设置‘四川’为当前选中值'>  
<input type='button' name='btn10' value='清空下拉框'>

</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值