×

jQuery的控件及事件(输入控件及回车事件)使用示例

作者:Terry2017.02.02来源:Web前端之家浏览:9421评论:0
关键词:JQueryJS
复制代码 代码如下:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>JavaScript</title>
<style type="text/css">
body { /* 对页面布局进行调整 */
background-color: #ccc;
width: 960px;
height: 50%;
margin: 0px auto;
}
</style>
</head>
<body>
<script type="text/javascript" src="jquery-1.8.0.js">
</script>

用户名:<input type="text" name="user" id="user"/><br/>
密   码:<input type="password" name="passwd" id="passwd"></input>

<script type="text/javascript">
$("body")
.css("margin-left",(($(document).width() - 1024) / 2) + "px")
.css("overflow-x", "hidden");

/* 只对id为passwd的控件绑定keypress事件 */
$("input[id='passwd']").bind("keypress", function(event){
if (event.keyCode == '13') { // 回车按键的代码
if (!$('#user').val() || !$('#passwd').val()) { // 检测用户名与密码是否为空
          alert("用户名或密码为空,请重新输入!");
          return;
        }
alert("用户名为:" + $('#user').val() + ", 密码为:" + $('#passwd').val());
}
});

</script>
</body>
</html>

您的支持是我们创作的动力!
温馨提示:本文作者系Terry ,经Web前端之家编辑修改或补充,转载请注明出处和本文链接:
https://jiangweishan.com/article/svg1485964800977.html

网友评论文明上网理性发言已有0人参与

发表评论: