jquery input选择弹框

时间:2023-03-09 04:33:28
jquery input选择弹框

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Insert title here</title>
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="jquery.plugin.js"></script>
<link rel="stylesheet" type="text/css" href="jquery.plugin.css" />
<script type="text/javascript">
jQuery.fn.selectCity = function(targetId) {
var _seft = this;
var targetId = $(targetId); this.click(function() {
var A_top = $(this).offset().top + $(this).outerHeight(true);
var A_left = $(this).offset().left;
targetId.bgiframe();
targetId.show().css({
"position" : "absolute",
"top" : A_top + "px",
"left" : A_left + "px"
});
}); targetId.find("#selectItemClose").click(function() {
targetId.hide();
}); targetId.find("#selectSub :checkbox").click(function() {
targetId.find(":checkbox").attr("checked", false);
$(this).attr("checked", true);
_seft.val($(this).val());
targetId.hide();
});
$(document).click(function(event) {
if (event.target.id != _seft.selector.substring(1)) {
targetId.hide();
}
}); targetId.click(function(e) {
e.stopPropagation();
}); return this;
}; $(function() {
$("#address").selectCity("#selectItem");
$("#address2").selectCity("#selectItem2");
});
</script>
</head>
<body> <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="text" name="address" id="address" size="33"><br> <div id="selectItem" class="selectItemhidden">
<div id="selectItemAd" class="selectItemtit bgc_ccc">
<h2 id="selectItemTitle" class="selectItemleft">请选择城市</h2>
<div id="selectItemClose" class="selectItemright">关闭</div>
</div>
<div id="selectItemCount" class="selectItemcont">
<div id="selectSub">
<input type="checkbox" name="cr01" id="cr01" value="北京1" />
<label for="cr01">北京1</label>
<input type="checkbox" name="cr02" id="cr02" value="北京2" />
<label for="cr02">北京2</label>
<input type="checkbox" name="cr03" id="cr03" value="北京3" />
<label for="cr03">北京3</label>
<input type="checkbox" name="cr09" id="cr09" value="北京4" />
<label for="cr09">北京4</label>
<input type="checkbox" name="cr09" id="cr09" value="北京4" />
<label for="cr09">北京4</label>
<input type="checkbox" name="cr09" id="cr09" value="北京4" />
<label for="cr09">北京4</label>
<input type="checkbox" name="cr09" id="cr09" value="北京4" />
<label for="cr09">北京4</label>
<input type="checkbox" name="cr09" id="cr09" value="北京4" />
<label for="cr09">北京4</label>
</div>
</div>
</div>
<!--
<input type="text" name="address2" id="address2" size="20">
<div id="selectItem2" class="selectItemhidden">
<div id="selectItemAd" class="selectItemtit bgc_ccc">
<h2 id="selectItemTitle" class="selectItemleft">请选择城市</h2>
<div id="selectItemClose" class="selectItemright">关闭</div>
</div>
<div id="selectItemCount" class="selectItemcont">
<div id="selectSub">
<input type="checkbox" name="cr04" id="cr04" value="北京1" />
<label for="cr04">北京1</label>
<input type="checkbox" name="cr05" id="cr05" value="北京2" />
<label for="cr05">北京2</label>
<input type="checkbox" name="cr06" id="cr06" value="北京3" />
<label for="cr06">北京3</label>
<input type="checkbox" name="cr07" id="cr07" value="北京4" />
<label for="cr07">北京4</label>
</div>
</div>
</div>
-->
</body>
</html>

jquery.plugin.css

body{font-size:12px;}

#selectItem{background:#FFF;position:absolute;top:0px;left:center;border:1px solid #000;overflow:hidden;width:240px;z-index:;}
.selectItemtit{line-height:20px;height:20px;margin:1px;padding-left:12px;}
.bgc_ccc{background:#E88E22;}
.selectItemleft{float:left;margin:0px;padding:0px;font-size:12px;font-weight:bold;color:#fff;}
.selectItemright{float:right;cursor:pointer;color:#fff;}
.selectItemhidden{display:none;}
.selectItemcont{padding:8px;}
/*.selectItemcls{clear:both;font-size:0px;height:0px;overflow:hidden;}*/
/*#selectItem2{background:#FFF;position:absolute;top:0px;left:center;border:1px solid #000;overflow:hidden;width:240px;z-index:1000;}*/

jquery.plugin.js

(function($){
$.fn.bgIframe = $.fn.bgiframe = function(s) {
// This is only for IE6
if ( $.browser.msie && /6.0/.test(navigator.userAgent) ) {
s = $.extend({
top : 'auto', // auto == .currentStyle.borderTopWidth
left : 'auto', // auto == .currentStyle.borderLeftWidth
width : 'auto', // auto == offsetWidth
height : 'auto', // auto == offsetHeight
opacity : true,
src : 'javascript:false;'
}, s || {});
var prop = function(n){return n&&n.constructor==Number?n+'px':n;},
html = '<iframe class="bgiframe"frameborder="0"tabindex="-1"src="'+s.src+'"'+
'style="display:block;position:absolute;z-index:-1;'+
(s.opacity !== false?'filter:Alpha(Opacity=\'0\');':'')+
'top:'+(s.top=='auto'?'expression(((parseInt(this.parentNode.currentStyle.borderTopWidth)||0)*-1)+\'px\')':prop(s.top))+';'+
'left:'+(s.left=='auto'?'expression(((parseInt(this.parentNode.currentStyle.borderLeftWidth)||0)*-1)+\'px\')':prop(s.left))+';'+
'width:'+(s.width=='auto'?'expression(this.parentNode.offsetWidth+\'px\')':prop(s.width))+';'+
'height:'+(s.height=='auto'?'expression(this.parentNode.offsetHeight+\'px\')':prop(s.height))+';'+
'"/>';
return this.each(function() {
if ( $('> iframe.bgiframe', this).length == 0 )
this.insertBefore( document.createElement(html), this.firstChild );
});
}
return this;
}; })(jQuery);

构建的js

var selectItemhidden = jQuery("<div></div>").addClass("selectItemhidden").attr("id","selectItem");

var selectItemtit = jQuery("<div></div>").addClass("selectItemtit bgc_ccc").attr("id","selectItemAd");
var selectItemleft = jQuery("<h2></h2>").addClass("selectItemleft").attr("id","selectItemTitle");
var selectItemright = jQuery("<div></div>").addClass("selectItemright").attr("id","selectItemClose"); var selectItemcont = jQuery("<div></div>").addClass("selectItemcont").attr("id","selectItemCount");
var selectSub = jQuery("<div></div>").attr("id","selectSub");
var checkbox = jQuery("<input>").attr("type","checkbox").attr("name","cr01").attr("id","cr01").val("北京1");
var label = jQuery("<label>").attr("for","cr01").html("北京"); var title = selectItemtit.append(selectItemleft).append(selectItemright);
var content = selectItemcont.append(selectSub.append(checkbox).append(label)); selectItemhidden.append(title).append(content);