【问题标题】:How to retrieve the data from a JSP textbox and display the data in a JQuery alert box in the same page?如何从 JSP 文本框中检索数据并在同一页面的 JQuery 警报框中显示数据?
【发布时间】:2018-08-14 09:54:48
【问题描述】:

我在 JSP 中创建一个简单的网页,我将在其中创建一个文本框和一个按钮。要求是一旦用户在输入一些文本后单击按钮,屏幕必须显示一个警报,显示输入的名称。我必须使用其中一个 JQuery 插件来实现这一点。以下是示例代码供您参考:

文件名:b.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
         pageEncoding="ISO-8859-1"%>
<%@page import="java.sql.Connection,java.sql.PreparedStatement,java.sql.DriverManager,java.sql.ResultSet" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <script src="jquery-confirm-master/dist/jquery-confirm.min.js"></script>
        <link  rel="stylesheet" href="jquery-confirm-master/dist/jquery-confirm.min.css">
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
        <title>JSP Test - b.jsp</title>
    </head>
    <body>
        <form action="b.jsp">
            <table border="1">
                <tr>
                    <td>Text:</td>
                    <td><input type="text" name="myText" id="myText" value=""></td>
                    <td><input type="Submit" value="Click to Submit"></td>
                </tr>
            </table>
        </form>
        <br>
        <%
            String myText = request.getParameter("myText");
            if (myText == null) {
                // myText is null when the page is first requested, 
                // so do nothing
            } else {
                if (myText.length() == 0) {
                    // There was a querystring like ?myText=
                    // but no text, so myText is not null, but 
                    // a zero length string instead.
        %>
        <b>myText is empty</b>
        <% } else {%>
        <b>myText is <%= myText%></b>
        <%
                }
            }
        %>
        <input type="button" id="alert" value="alert me" onclick="bn()">
        <script>
            function bn() {
                $.alert({
                    title: 'Alert!',
                    content: 'Simple alert!',
                });
            }
        </script>
    </body>
</html>

在上面的代码中,我创建了单独的按钮“警报”来显示警报框(使用 jquery 插件),但是如果我想在 JQuery 警报中的文本框 id=myText 中显示带有输入文本的警报框怎么办盒子。

【问题讨论】:

    标签: java jquery jsp jquery-plugins


    【解决方案1】:

    在您的条件下添加脚本。

    <% } else {%>
        <script>
            $.alert({
                title: 'Alert!',
                content: '<%= myText%>',
            });
        </script>
    <% }%>
    

    【讨论】:

    • 谢谢..正是我想要的。
    • 我遇到了一个问题。看看我更新的代码,最后我显示了带有表格视图的 JQuery 警报框,其中显示了我从数据库中检索的 ID、姓名和薪水。这里id和salary被获取并显示在表格中。但是值名称,即 myText 没有显示。每次我在警报内容中插入 myText 时,浏览器都不会显示任何警报框。收到错误“未捕获的 ReferenceError:未定义”。
    • 尝试不使用字符串连接。例如:'&lt;tr&gt;&lt;td&gt;&lt;%=b%&gt;&lt;/td&gt;'。如果问题仍然存在。请将其添加为单独的问题。
    • 我回滚了您的编辑,因为它与原始问题和接受的答案不匹配。请将您的下一个问题添加为新问题。它可以帮助未来的读者一次理解一个问题。
    猜你喜欢
    • 2016-01-25
    • 2013-03-23
    • 2013-04-23
    • 2017-10-24
    • 2015-02-28
    相关资源
    最近更新 更多