jQuery操作checkbox,radio, select 功能
2009-10-29 10:43
633 查看
要引用jqery.js
<script>
$(function () {
// 全选
$('#hobby_all').click(function () {
$('input[name="hobby[]"]').attr("checked",true);
}) ;
// 反选
$('#hobby_anti_all').click(function () {
$('input[name="hobby[]"]').attr("checked",false);
}) ;
// 全选
$('#city_all').click(function () {
$('input[name="city[]"]').attr("checked",true);
}) ;
// 反选
$('#city_anti_all').click(function () {
$('input[name="city[]"]').attr("checked",false);
}) ;
$('#submit').click(function () {
// 获取多选框的值
hobby = '<br>兴趣爱好: <br>' ;
$('input[@name="hobby[]"]').each(function () {
if ($(this).attr('checked') == true) {
hobby += $(this).attr('value')+",<br>" ;
}
});
// 获取多选框的值
city = '<br><br>喜欢的城市: <br>' ;
$('input[@name="city[]"]').each(function () {
if ($(this).attr('checked') == true) {
city += $(this).attr('value')+",<br>" ;
}
});
// 获取下拉列表的值
age = '<br><br>年龄: <br>' + $('#age').val() ;
// 获取单选框的值
sex = '<br><br>性别: <br>' + $('input[@type="radio"][@checked]').val() ;
$('.content').html(hobby + city + age + sex) ;
});
});
</script>
<style>
.fieldset{
width: 200px;
}
</style>
<div class='test' id='checkbox'>
<form method="post" >
性别
<input type="radio" name="sex" value="male" />男
<input type="radio" name="sex" value="female" />女
<input type="radio" name="sex" value="null" />保密
<br />
<br />
年龄:
<select name="age" id="age" value="年龄">
<option value="20">20</option>
<option value="30" >30</option>
<option value="40" >40</option>
<option value="50">50+</option>
</select>
<br />
<br />
<fieldset class="fieldset">
<legend>兴趣爱好 </legend>
旅游<input type="checkbox" name="hobby[]" id="tour" value="tour" /><br />
音乐<input type="checkbox" name="hobby[]" id="music" value="music" /><br />
足球<input type="checkbox" name="hobby[]" id="football" value="football" /><br />
网游<input type="checkbox" name="hobby[]" id="game" value="game" /><br />
<input type="button" name="hobby_anti_all" id="hobby_anti_all" value="反选"/>
<input type="button" name="hobby_all" id="hobby_all" value="全选" />
</fieldset>
<br />
<fieldset class="fieldset">
<legend>喜欢的城市</legend>
北京<input type="checkbox" name="city[]" id="beijing" value="beijing" /><br />
上海<input type="checkbox" name="city[]" id="shanghai" value="shanghai" /><br />
天津<input type="checkbox" name="city[]" id="tianjin" value="tianjin" /><br />
重庆<input type="checkbox" name="city[]" id="chongqing" value="chongqing" /><br />
<input type="button" name="city_anti_all" id="city_anti_all" value="反选"/>
<input type="button" name="city_all" id="city_all" value="全选" />
</fieldset>
<br /><input type="button" id="submit" value="sumit" />
</form>
<div class="content"></div>
</div>
<script>
$(function () {
// 全选
$('#hobby_all').click(function () {
$('input[name="hobby[]"]').attr("checked",true);
}) ;
// 反选
$('#hobby_anti_all').click(function () {
$('input[name="hobby[]"]').attr("checked",false);
}) ;
// 全选
$('#city_all').click(function () {
$('input[name="city[]"]').attr("checked",true);
}) ;
// 反选
$('#city_anti_all').click(function () {
$('input[name="city[]"]').attr("checked",false);
}) ;
$('#submit').click(function () {
// 获取多选框的值
hobby = '<br>兴趣爱好: <br>' ;
$('input[@name="hobby[]"]').each(function () {
if ($(this).attr('checked') == true) {
hobby += $(this).attr('value')+",<br>" ;
}
});
// 获取多选框的值
city = '<br><br>喜欢的城市: <br>' ;
$('input[@name="city[]"]').each(function () {
if ($(this).attr('checked') == true) {
city += $(this).attr('value')+",<br>" ;
}
});
// 获取下拉列表的值
age = '<br><br>年龄: <br>' + $('#age').val() ;
// 获取单选框的值
sex = '<br><br>性别: <br>' + $('input[@type="radio"][@checked]').val() ;
$('.content').html(hobby + city + age + sex) ;
});
});
</script>
<style>
.fieldset{
width: 200px;
}
</style>
<div class='test' id='checkbox'>
<form method="post" >
性别
<input type="radio" name="sex" value="male" />男
<input type="radio" name="sex" value="female" />女
<input type="radio" name="sex" value="null" />保密
<br />
<br />
年龄:
<select name="age" id="age" value="年龄">
<option value="20">20</option>
<option value="30" >30</option>
<option value="40" >40</option>
<option value="50">50+</option>
</select>
<br />
<br />
<fieldset class="fieldset">
<legend>兴趣爱好 </legend>
旅游<input type="checkbox" name="hobby[]" id="tour" value="tour" /><br />
音乐<input type="checkbox" name="hobby[]" id="music" value="music" /><br />
足球<input type="checkbox" name="hobby[]" id="football" value="football" /><br />
网游<input type="checkbox" name="hobby[]" id="game" value="game" /><br />
<input type="button" name="hobby_anti_all" id="hobby_anti_all" value="反选"/>
<input type="button" name="hobby_all" id="hobby_all" value="全选" />
</fieldset>
<br />
<fieldset class="fieldset">
<legend>喜欢的城市</legend>
北京<input type="checkbox" name="city[]" id="beijing" value="beijing" /><br />
上海<input type="checkbox" name="city[]" id="shanghai" value="shanghai" /><br />
天津<input type="checkbox" name="city[]" id="tianjin" value="tianjin" /><br />
重庆<input type="checkbox" name="city[]" id="chongqing" value="chongqing" /><br />
<input type="button" name="city_anti_all" id="city_anti_all" value="反选"/>
<input type="button" name="city_all" id="city_all" value="全选" />
</fieldset>
<br /><input type="button" id="submit" value="sumit" />
</form>
<div class="content"></div>
</div>
相关文章推荐
- jQuery操作 checkbox 的全选、反选 , radio, select 功能
- jQuery操作 checkbox 的全选、反选 , radio, select 功能
- jQuery对radio,checkbox,select元素的操作
- JQuery对基本Html控件的操作TextBox、Select、Radio、CheckBox
- Jquery 操作Html 控件 CheckBox、Radio、Select 控件
- Jquery 操作Html 控件 CheckBox、Radio、Select 控件
- jquery操作select、checkbox、radio!(每次写都需要去查,还是总结一下吧)
- jQuery对select、radio和checkbox的操作
- jquery下的 radio,checkbox,select操作
- jQuery操作radio、checkbox、select总结
- jQuery对select、checkbox、radio操作小结
- jquery 操作select,checkbox,radio (整理)
- jquery对radio,checkbox,select的操作
- jquery实用应用之jquery操作radio、checkbox、select选中
- Jquery操作select、checkbox、radio详细讲解
- jQuery操作radio、checkbox、select示例
- jquery 操作 select,radio,checkbox等(转载)
- jQuery中下拉select、复选checkbox、单选radio的操作代码
- jQuery操作radio、checkbox、select总结
- jquery radio、 checkbox、 select 操作