JQuery简单的按钮点击和文本框输入事件
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% 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> <base href="<%=basePath%>"> <title>My JSP 'test.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <script type="text/javascript" src="script/jquery-1.8.3.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#clickme").click(function(){ alert("Hello World click"); }); $('#clickmebind').bind("click", function(){ alert("Hello World bind"); }); $('#clickmeon').on('click', function(){ alert("Hello World on"); }); }); </script> <script type="text/javascript"> //使用id的方式获取 $(document).ready(function(){ //1 $("#button_text1").click(function(){ var result1 = $("#input_text1").val(); alert("result1 = " + result1); }); //2 $("#button_text2").click(function(){ var result2 = $("input[id='input_text2']").val(); alert("result2 = " + result2); }); //3 $("#button_text3").click(function(){ var result3 = $("input[id='input_text3']").attr("value"); alert("result3 = " + result3); }); //4. 可以通过type的值来获取input中的值(未演示) /* $("#button_text4").click(function(){ var result4 = $("input[type='text']").val(); alert("result4 = " + result4); }); */ //5. 可以通过name的值来获取input中的值(未演示) /* $("#button_text5").click(function(){ var result5 = $("input[name='text']").val(); alert("result5 = " + result5); }); */ }); </script> </head> <body> <h1>根据ID来判定</h1> <input type="button" name="clickmes" id="clickme" value="click me" /> <input type="button" name="clickmebinds" id="clickmebind" value="clickme_bind" /> <input type="button" name="clickmeons" id="clickmeon" value="clickme_on" /> <!-- 获取文本框的值:方式一 --> <div id="test1"> <input id="input_text1" type="text" value="test1" style="width: 100px;" /> <button id="button_text1">test1</button> </div> <!-- 获取文本框的值:方式二 --> <div id="test2"> <input id="input_text2" type="text" value="test2" style="width: 100px;" /> <button id="button_text2">test2</button> </div> <!-- 获取文本框的值:方式三 --> <div id="test3"> <input id="input_text3" type="text" value="test3" style="width: 100px;" /> <button id="button_text3">test3</button> </div> </body> </html>