Jquery实现弹出选择框选择后返回,支持多级分类

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <title>选择收件人</title>
  <script src="../jquery-1.11.1.min.js" type="text/javascript"></script>
  <script src="friend.js" type="text/javascript"></script>
 </head>

 <body>
	收件人:<input type="text" name="my-name" id="my-name" value="请选择收件人" onclick="pop()" />
           <input type="hidden" name="my-value" id="my-value" />

    <!--alert layer-->
	<div id="choose-box-wrapper">
	  <div id="choose-box">
		<div id="choose-box-title">
			<span>选择好友</span>
		</div>
		<div id="choose-a-class">
		</div>
		<div>
            <ul id="choose-a-item">
            </ul>
		</div>
		<div id="choose-box-bottom">
            <input type="button" onclick="doSelect()" value="确定" />  
			<input type="button" onclick="hide()" value="关闭" />
		</div>
	  </div>
	</div>
    <!--alert layer-->

  <script type="text/javascript">
	//弹出窗口
	function pop(){
		//将窗口居中
		makeCenter();
		//初始化分类列表
		initLayerClass();
		//默认情况下, 给第一个分类添加choosen样式
		$('[class-id="1"]').addClass('choosen');
		//初始化ITEM列表
		initItemList(1);
	}

	//隐藏窗口
	function hide() {
	    $('#choose-box-wrapper').css("display", "none");
	}

    //获取选择值
	function doSelect() {
	    var list = $(".class-item-ck");
	    var text = "";
	    var value = "";
	    list.each(function () {
	        if ($(this).is(':checked')) {
	            text += $(this).attr("item-name") + ";";
	            value += $(this).attr("item-id");
	        };
	    });
	    $('#my-name').val('').val(text);
	    $('#my-value').val('').val(value);
	    //关闭弹窗
	    hide();
    };

	function initLayerClass()
	{
		//原先的分类列表清空
		$('#choose-a-class').html('');
		for (i = 0; i < itemList.length; i++) {
		    $('#choose-a-class').append('<a class="class-item" class-id="' + itemList[i].id + '">' + itemList[i].name + '</a>');
		}
		//添加分类列表项的click事件
		$('.class-item').bind('click', function () {
		    var item = $(this);
		    var classid = item.attr('class-id');
		    var choosenItem = item.parent().find('.choosen');
		    if (choosenItem) {
		        $(choosenItem).removeClass('choosen');
		    }
		    item.addClass('choosen');
		    //更新列表
		    initItemList(classid);
		}
		);
	}

	function initItemList(classid)
	{
		//原先列表清空
		$('#choose-a-item').html('');
		var mitems = itemList[classid - 1].items;
		for (i = 0; i < mitems.length; i++) {
		    var html = '<li class="itemli">';
		    html += '<a class="mdata-item" item-id="' + mitems[i].id + '">' + mitems[i].name + '</a>';
		    html += '<input type="checkbox" class="class-item-ck" item-id="' + mitems[i].id + '" item-name="' + mitems[i].name + '" />';
		    html += '</li>';
		    $('#choose-a-item').append(html);
		}
		//添加详细列表项的click事件
		$('.mdata-item').bind('click', function(){
				var item=$(this);
				//更新选择文本框中的值
				$('#my-name').val(item.text());
				//关闭弹窗
				hide();
			}
		);
	}

	function makeCenter()
	{
		$('#choose-box-wrapper').css("display","block");
		$('#choose-box-wrapper').css("position","absolute");
		$('#choose-box-wrapper').css("top", Math.max(0, (($(window).height() - $('#choose-box-wrapper').outerHeight()) / 2) + $(window).scrollTop()) + "px");
		$('#choose-box-wrapper').css("left", Math.max(0, (($(window).width() - $('#choose-box-wrapper').outerWidth()) / 2) + $(window).scrollLeft()) + "px");
	}

  </script>

  <style type="text/css">
	#choose-box-wrapper{
		width: 652px;
		background-color:#000;
		filter:alpha(opacity=50);
		background-color: rgba(0, 0, 0, 0.5);
		padding:10px;
		border-radius:5px;
		display:none;
	}
	#choose-box{
		border: 1px solid #005EAC;
		width:650px;
		background-color:white;
	}
	#choose-box-title{
		background:#3777BC;
		color: white;
		padding: 4px 10px 5px;
		font-size: 14px;
		font-weight: 700;
		margin: 0;
	}
	#choose-box-title span{
		font-family: Tahoma, Verdana, STHeiTi, simsun, sans-serif;
	}

	#choose-a-class, #choose-a-item{
		margin:5px 8px 10px 8px;
		border: 1px solid #C3C3C3;
	}
	#choose-a-class a{
		display:inline-block;
		height: 18px;
		line-height: 18px;
		color:#005EAC;
		text-decoration: none;
		font-size: 9pt;
		font-family: Tahoma, Verdana, STHeiTi, simsun, sans-serif;
		margin:2px 5px;
		padding: 5px;
		text-align: center;
	}
	#choose-a-class a:hover{
		text-decoration: none;
		cursor:pointer;
	}
	#choose-a-class .choosen{
		background-color: #005EAC;
		color:white;
	}

	#choose-a-item{
		overflow-x: hidden;
		overflow-y: auto;
		height: 200px;
	}
	#choose-a-item a{
		height: 18px;
		line-height: 18px;
		color:#005EAC;
		text-decoration: none;
		font-size: 9pt;
		font-family: Tahoma, Verdana, STHeiTi, simsun, sans-serif;
		float: left;
		padding:2px 5px 2px 5px;
		margin: 4px;
		padding-left:10px;
		background:url(2012072500060712.gif) no-repeat 0 9px;
	}
	#choose-a-item a:hover{
		background-color:#005EAC;
		color:white;
		cursor:pointer;
	}
	#choose-a-item .itemli{
	    width:100px;
	    list-style:none;
	    float:left;
	}
	#choose-a-item .class-item-ck
	{
	    margin-top:10px;
	    display:inline;
	}

	#choose-box-bottom{
		background: #F0F5F8;
		padding: 8px;
		text-align: right;
		border-top: 1px solid #CCC;
		height:40px;
	}
	#choose-box-bottom input{
		vertical-align: middle;
		text-align: center;
		background-color:#005EAC;
		color:white;
		border-top: 1px solid #B8D4E8;
		border-left: 1px solid #B8D4E8;
		border-right: 1px solid #114680;
		border-bottom: 1px solid #114680;
		cursor: pointer;
		width: 60px;
		height: 25px;
		margin-top: 6px;
		margin-right: 6px;
	}
  </style>

 </body>
</html>
var itemList = [
{
    "id": 1,
    "items": [{
        "id": 1001,
        "name": "张三1"
    },
    {
        "id": 1002,
        "name": "李四1"
    },
    {
        "id": 1003,
        "name": "王五1"
    },
    {
        "id": 1004,
        "name": "赵六1"
    },
    {
        "id": 1005,
        "name": "赵七1"
    }],
    "name": "我的同学"
},
{
    "id": 2,
    "items": [{
        "id": 1001,
        "name": "张三2"
    },
    {
        "id": 1002,
        "name": "李四2"
    },
    {
        "id": 1003,
        "name": "王五2"
    },
    {
        "id": 1004,
        "name": "赵六2"
    },
    {
        "id": 1005,
        "name": "赵七2"
    }],
    "name": "我的同事"
}
]

时间: 2024-07-30 20:15:24

Jquery实现弹出选择框选择后返回,支持多级分类的相关文章

C# GridView Edit &amp; Delete, 点击Delete的时候弹出确认框

1. 使用GridView自带属性ShowEditButton和ShowDeleteButton,均设为True  <Columns> ... <asp:CommandField ShowEditButton="True" ShowDeleteButton="True"></asp:CommandField> </Columns> 则在GridView指定列的位置会显示Edit和Delete的LinkButton 2.

点击文本框弹出可供选择的checkbox复选框代码实例

点击文本框弹出可供选择的checkbox复选框代码实例:本章节分享一段代码实例,它能够点击文本框的时候,能够弹出下拉的checkbox复选框,选中复选框就能够将值写入文本框中,可能在实际应用中的效果没有这么直白简单,不过可以作为一个例子演示,以便于学习者理解和扩展.代码如下: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="author&qu

jQuery - 选中复选框则弹出提示框

<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>判断是否选中</title> <script type="text/javascript" src="./js/jquery-1.8.3.min.js"></script> <script

.net 弹出消息框后,页面样式变乱

点击按钮,执行提交操作,弹出消息框后,页面的样式变乱,解决方法: 首先,确定使用的css样式正确,页面中的宽高值保持规范统一: 然后,弹出框避免使用Response.Write(),如下所示 Response.Write("<script language=javascript>"); Response.Write("window.alert('"); Response.Write("计划添加失败!"); Response.Write

基于Jquery 简单实用的弹出提示框

引言: 原生的 alert 样子看起来很粗暴,网上也有一大堆相关的插件,但是基本上都是大而全,仅仅几句话可以实现的东西,可能要引入好几十k的文件,所以话了点时间自己写了个弹出效果,放到项目上去发现效果还不错,这里贴出来,做个备忘,有需要的同学可以拿去,也可以作为一个参考. 1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.d

基于jQuery鼠标点击弹出登陆框效果

基于jQuery鼠标点击弹出登陆框效果.这是一款扁平样式风格的jQuery弹出层登陆框特效.效果图如下: 在线预览   源码下载 实现的代码. html代码: <input type="button" class="one" value="点击我查看效果" /> <div class="box"> <div class="box2"> <div class=&quo

JQuery实现感应鼠标弹出气泡框悬停提示的js特效

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-

js简单显示和隐藏div,触发超链接,动态更改button值,setInterval()简单使用,jquery easyui弹出框简单使用 .

js简单显示和隐藏div 01.<!DOCTYPE html> 02.<html> 03.<head> 04.<meta charset="UTF-8"> 05.<title>Insert title here</title> 06.<script type="text/javascript"> 07. window.onload=function(){ 08. document.g

layer弹出信息框API

首先向大家推荐layer,在这里也非常感谢贤心的贡献,非常不错的信息框及弹出层解决方案,为一些项目的前端开发提高了很大的效率,希望layer 越办越好! 下面是API,呵呵,官方抄袭过来的,为了自己看着方便. layer API之键值(仅在需要时配置,未配置的会用默认) 键 : 值 描述 skin : 0 层的皮肤编号,值为整数型.目前默认只提供一种皮肤,当你按照样式规则新增皮肤时(详见skin/layer.css),你可配置该参数. type : 0, 层的类型.0:信息框(默认),1:页面层

js弹出提示框并跳转页面

1.提示框有两个选择项,点确定跳转,取消停留在原页面ScriptManager.RegisterStartupScript(Page, this.GetType(), "", "<script>if(confirm('请登录?')){location.href='login.aspx'};</script>", false); 2.提示框只有一个确定按钮,跳转到指定页面ScriptManager.RegisterStartupScript(p