<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs
/jquery/1.4.0/jquery.min.js"></script>
<base href="<%=basePath%>">
<title>My JSP 'MyJsp.jsp' starting page</title>
</head>
<body>
This is my JSP page. <br>
<s:form id="loginForm">
<s:textfield name="user" label="用户名"/>
<s:textfield name="pass" label="密码"/>
<tr><td colspan="2">
<input id="loginBn" type="button" value="提交"/>
</td></tr>
</s:form>
<div id="show" style="display:none;">
</div>
<script type="text/javascript">
$("#loginBn").onclick=function(){
var value=document.getElementById("loginBn").value
alert(value)
//alert($("#loginBn"))
}</script>
</body>
</html>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs
/jquery/1.4.0/jquery.min.js"></script>
<base href="<%=basePath%>">
<title>My JSP 'MyJsp.jsp' starting page</title>
</head>
<body>
This is my JSP page. <br>
<s:form id="loginForm">
<s:textfield name="user" label="用户名"/>
<s:textfield name="pass" label="密码"/>
<tr><td colspan="2">
<input id="loginBn" type="button" value="提交"/>
</td></tr>
</s:form>
<div id="show" style="display:none;">
</div>
<script type="text/javascript">
$("#loginBn").onclick=function(){
var value=document.getElementById("loginBn").value
alert(value)
//alert($("#loginBn"))
}</script>
</body>
</html>
alert($("#loginBn").val())
$("#loginBn").onclick(function(){
var value=document.getElementById("loginBn").value
alert(value)
});
})
$("#loginBn").click(function(){
var value=document.getElementById("loginBn").value
alert(value)
//alert($("#loginBn"))
});
这一行代码有问题,$("#loginBn")获得的是jQuery的对象,而onclick调用的是dom对象的函数,解决的办法是调用jquery的函数:
$("#loginBn").click(function(){
var value=document.getElementById("loginBn").value
alert(value)
//alert($("#loginBn"))
})
但这样还不够,为了让这个函数被加载,需要用$(function(...))的形式把它包起来,所以完整的代码是
$(function(
$("#loginBn").click(function(){
var value=document.getElementById("loginBn").value
alert(value)
//alert($("#loginBn"))
});
))
$(function(
$("#loginBn").click(function(){
var value=$(this).val();
alert(value)
});
));