jquery复选框多选赋值给文本框的方法
更新时间:2015年01月27日 10:19:26 投稿:shichen2014
这篇文章主要介绍了jquery复选框多选赋值给文本框的方法,涉及jQuery相关选择器的使用技巧,需要的朋友可以参考下
本文实例讲述了jquery复选框多选赋值给文本框的方法。分享给大家供大家参考。具体实现方法如下:
复制代码 代码如下:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>jquery点击复选框触发事件给input赋值</title><base target="_blank" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<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>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>jquery点击复选框触发事件给input赋值</title><base target="_blank" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<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程序设计有所帮助。
相关文章
jQuery实现可以编辑的表格实例详解【附demo源码下载】
这篇文章主要介绍了jQuery实现可以编辑的表格,涉及jQuery响应鼠标事件动态操作页面元素的相关技巧,需要的朋友可以参考下2016-07-07jQuery getJSON()+.ashx 实现分页(改进版)
参考了上一篇Asp .net +jquery +.ashx 文件实现分页并作了改进:ashx返回json数据,减少传输数据量,html页面样式控制也比较灵活,感兴趣的朋友可以参考下哈2013-03-03PHP+MySQL+jQuery随意拖动层并即时保存拖动位置实例讲解
这篇文章主要介绍了PHP+MySQL+jQuery随意拖动层并即时保存拖动位置的实现方法,感兴趣的小伙伴们可以参考一下2015-10-10Jquery ajax执行顺序 返回自定义错误信息(实例讲解)
由于Jquery中的Ajax的async默认是true(异步请求),如果想一个Ajax执行完后再执行另一个Ajax, 需要把async=false就可以了2013-11-11利用jQuery及AJAX技术定时更新GridView的某一列数据
这篇文章主要介绍了利用jQuery及AJAX技术定时更新GridView的某一列数据的方法,这里的GridView是指C#软件开发中的GridView控件,需要的朋友可以参考下2015-12-12
最新评论