jquery实现勾选复选框触发事件给input赋值
更新时间:2015年02月01日 10:19:37 投稿:hebedich
本文给大家介绍的是一段十分实用的代码,使用jQuery实现勾选复选框触发事件给input赋值,在制作项目的时候经常需要用到此功能,这里分享给大家。
代码如下:
复制代码 代码如下:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>jquery点击复选框触发事件给input赋值-柯乐义</title><base target="_blank" />
<style type="text/css">
* {
margin: 0;
padding: 0;
list-style-type: none;
}
a, img {
border: 0;
text-decoration: none;
}
body {
font: 12px/180% Arial, Helvetica, sans-serif, "新宋体";
}
table {
empty-cells: show;
border-collapse: collapse;
border-spacing: 0;
}
/* tablist */
.tablist {
width: 400px;
border: solid 8px #ddd;
margin: 40px auto;
}
.tablist td {
line-height: 24px;
border-bottom: solid 1px #ddd;
text-align: left;
padding: 10px;
}
.tablist td input {
line-height: 20px;
margin-left: 5px;
}
.tablist td .txtValue
{
padding: 3px 0;
width: 180px;
}
</style>
</head>
<body>
<table cellpadding="0" cellspacing="0" class="tablist">
<tr>
<td><input class="txtValue" type="text" name="keleyi" value="" /> <input type="checkbox" data-type="checkall" />全选</td>
</tr>
<tr>
<td>
<input type="checkbox" name="keleyi" data-type="checkbox" data-value="张三" value="1" />张三
<input type="checkbox" name="keleyi" data-type="checkbox" data-value="李四" value="2" />李四
<input type="checkbox" name="keleyi" data-type="checkbox" data-value="柯乐义" value="3" />柯乐义
<input type="checkbox" name="keleyi" data-type="checkbox" data-value="赵六" value="4" />赵六
</td>
</tr>
</table>
<script type="text/javascript" src="jquery/jquery-1.11.2.min.js"></script>
<script type="text/javascript">
$(function(){
$('[data-type="checkbox"]').click(function(){
var data_value = $(this).attr('data-value'),
txtalso = $.trim($(".txtValue").val());
if($(this).prop("checked")) {
if(txtalso.length > 0) {
if(txtalso.indexOf(data_value+',') != -1) {
return ;
} else {
txtalso += data_value + ',';
}
} else {
txtalso = data_value+',';
}
} else {
if(txtalso.indexOf(data_value+',') != -1) {
txtalso = txtalso.replace(data_value+',', '');
}
}
$(".txtValue").val(txtalso);
});
$('[data-type="checkall"]').click(function(){
var str = '';
if($(this).prop("checked")) {
$.each($('[data-type="checkbox"]'), function(i){
str += $(this).attr('data-value') + ',';
});
$('[data-type="checkbox"]').prop('checked', true);
} else {
$('[data-type="checkbox"]').prop('checked', false);
}
$(".txtValue").val(str);
});
});
</script>
</body>
</html>
以上就是本代码的全部了,小伙伴们自由扩展,美化,希望大家能够喜欢。
相关文章
jQuery Ajax Post 回调函数不执行问题的解决方法
本文是小编给大家带来的jQuery Ajax Post 回调函数不执行的原因及解决方法,在本文最下面还给大家附加jquery Ajax 不执行回调函数success的原因,这两个问题都非常多见,感兴趣的朋友一起看下吧2016-08-08
jQuery.autocomplete 支持中文输入(firefox)修正方法
jQuery.autocomplete 是jquery的流行插件,,能够很好的实现输入框的自动完成(autocomplete)、建议提示(input suggest)功能,支持ajax数据加载。2011-03-03
jQuery实现的Tab滑动选项卡及图片切换(多种效果)小结
这篇文章主要介绍了jQuery实现的Tab滑动选项卡及图片切换效果小结,实例总结了几种常见的tab切换效果,包括鼠标点击切换、滑过切换、定时自动切换等,非常具有实用价值,需要的朋友可以参考下2015-09-09


最新评论