jquery textbox dropdwonlist

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" " http://www.w3.org/TR/html4/loose.dtd">
<html xmlns=" http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript" src=" http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js">
</script>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>New Web Project</title>
<script type="text/javascript" language="JavaScript">
$(document).ready(function(){
$('#test').val('');
//定义一个下拉按钮层,并配置样式(位置,定位点坐标,大小,背景图片,Z轴),追加到文本框后面
$DIV = $('<div></div>').css('position', 'absolute').css('left', $('#test').position().left + $('#test').width() - 15 + 'px').css('top',
$('#test').position().top + 2 + 'px').css('background', 'transparent url(/jzxy/UploadFiles_333/201110/20111016173708849.gif) no-repeat top left').css('height', '16px').css('width',
'15px').css('z-index', '100');
$('#test').after($DIV);
//定义一个下拉框层,并配置样式(位置,定位点坐标,宽度,Z轴),先将其隐藏
$SELECT = $('<div></div>').css('position', 'absolute').css('border', '1px solid #000000').css('left', $('#test').position().left + 'px').css
('top', $('#test').position().top + $('#test').height() + 7 + 'px').css('width', $('#test').width() + 'px').css('z-index', '100');
$('#test').after($SELECT);
$SELECT.hide();
//定义五个选项层,并配置样式(宽度,Z轴一定要比下拉框层高),添加name、value属性,加入下拉框层
for (var i = 0; i <= 5; i++) {
$OPTION = $('<div>option' + i + '</div>').attr('name', 'option').attr('value', 'value' + i).css('width', $SELECT.width()).css('z-index',
$SELECT.css('z-index') + 1);
$SELECT.append($OPTION);
};
//选项层的鼠标移入移出样式
$SELECT.mouseover(function(event){
if ($(event.target).attr('name') == 'option') {
//移入时背景色变深,字色变白
$(event.target).css('background-color', '#000077').css('color', 'white');
$(event.target).mouseout(function(){
//移出是背景色变白,字色变黑
$(event.target).css('background-color', '#FFFFFF').css('color', '#000000');
});
}
});
//鼠标进入修改背景图位置
$DIV.mouseover(function(){
$DIV.css('background-position', ' 0% -16px');
});
//鼠标移出修改背景图位置
$DIV.mouseout(function(){
$DIV.css('background-position', ' 0% -0px');
});
//鼠标按下修改背景图位置
$DIV.mousedown(function(){
$DIV.css('background-position', ' 0% -32px');
});
//鼠标释放修改背景图位置
$DIV.mouseup(function(){
$DIV.css('background-position', ' 0% -16px');
$SELECT.show();
});
//通过点击位置,判断弹出的显示
$(document).mouseup(function(event){
//如果是下拉按钮层或下拉框层,则依然显示下拉框层
if (event.target == $SELECT.get(0) || event.target == $DIV.get(0)) {
$SELECT.show();
}
else {
//如果是选项层,则改变文本框的值
if ($(event.target).attr('name') == 'option') {
//弹出value观察
alert($(event.target).attr('value'));
$('#test').val($(event.target).html());
}
//如果是其他位置,则将下拉框层
if ($SELECT.css('display') == 'block') {
$SELECT.hide();
}
}
});
});
</script>
</head>
<body>
<input type="text" id="test" name="test" />
<button type="button" οnclick="alert($('#test').serialize())">提交</button>
运行后,需要刷新一下, 才可以加载jquery
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值