Javascript 当我们使用jsp从数据库获取两个文本框中的相同值时,该怎么办

Javascript 当我们使用jsp从数据库获取两个文本框中的相同值时,该怎么办,javascript,java,jsp,Javascript,Java,Jsp,这是我的jsp代码 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ page import="java.sql.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/T

这是我的jsp代码

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ page import="java.sql.*" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Drop Downlist</title>

    <script type="text/javascript">

    function getprodqty(selectObject) {
        var value = selectObject.value;  
       //alert(value);
      // document.getElementById("id1").value=document.getElementById("id").value;
       document.getElementById("qut").value=document.getElementById("id").value;
       document.getElementById("price").value=document.getElementById("id").value;


    }
    function calc() {
        var alq = document.getElementById('alq').value;
        var price = document.getElementById('price').value;

        document.getElementById('total').value = alq * price;
    }

    </script>
    <script>
    function myFunction() {
        window.print();
    }
    </script>

    </head>
    <body>
    <%! String driverName = "com.mysql.jdbc.Driver";%>
    <%!String url = "jdbc:mysql://localhost:3306/ab";%>
    <%!String user = "abhishek";%>
    <%!String psw = "admin";%>
    <form action="#" method="post" name="form1">
    <%
    Connection con = null;
    PreparedStatement ps = null;
    try
    {
    Class.forName(driverName);
    con = DriverManager.getConnection(url,user,psw);
    String sql = "SELECT * FROM pro";
    ps = con.prepareStatement(sql);
    ResultSet rs = ps.executeQuery(); 
    %>
    <center><p>Select Name :
    <select name="prodqty" id="id" onchange="getprodqty(this)">

    <%
    while(rs.next())
    {
    String pname = rs.getString("pname"); 
    //int id=rs.getInt("pid");
    int pqty=rs.getInt("pqty");
    int price=rs.getInt("price");
    %>


    <option value="<%=pqty %>"><%=pname %></option>
    <%
    }
    %>
    </select>
    </p></center>

    <center>
    <a style="color:black"><b>Product ID: <input type="text" name="id" id="id1" /></b></a></br>
    <a style="color:black"><b>Available Quantity: <input type="text" name="pqty" id="qut" /></b></a><br /></br>
    </br>
    <center><a style="color:black"><b>Allocate Quantity: <input type="text" name="alq" id="alq" onkeyup="calc()" value=""/></b></a><br /></br>
    </br>
    <center><a style="color:black"><b>Price: &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp<input type="text" name="price" id="price" onkeyup="calc()" value="" /></b></a><br /></br>
    </br>
    <center><a style="color:black"><b>Total: &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp<input type="text" name="total" id="total" /></b></a><br /></br>
    </br>
    <a style="color:black"><b>Date: <input type="date" name="date" id="date" /></br>
    <center><button type="submit" class="button button1">SUBMIT</button></center><br>

    <center><button onclick="myFunction()">Print</button></center><br>
    <%
    }
    catch(SQLException sqe)
    { 
    out.println(sqe);
    }
    %>
    </form>
    </body>
    </html>

下拉列表
函数getprodqty(selectObject){
var value=selectObject.value;
//警报(值);
//document.getElementById(“id1”).value=document.getElementById(“id”).value;
document.getElementById(“qut”).value=document.getElementById(“id”).value;
document.getElementById(“price”).value=document.getElementById(“id”).value;
}
函数计算(){
var alq=document.getElementById('alq').value;
var price=document.getElementById('price').value;
document.getElementById('total')。value=alq*价格;
}
函数myFunction(){
window.print();
}
选择名称:

产品标识:
可用数量:


分配数量:


价格:


总计:总计


日期:
提交
打印

我从数据库中获取的数量和价格值是相同的,我需要不同的

您没有在代码中的任何地方打印价格变量,我想知道您是如何在JSP中检查它的


请您提供截图,让我们知道是什么问题,没有得到正确的问题。一旦您提供了更多详细信息,我就可以编辑我的答案。

您有一个选项可以获得这样的目标更改代码

while(rs.next())
    {
    String pname = rs.getString("pname"); 
    int id=rs.getInt("pid");
    int pqty=rs.getInt("pqty");
    int price=rs.getInt("price");
    %>


    <option value="<%=id %>"><%=pname %></option>
while(rs.next())
{
字符串pname=rs.getString(“pname”);
int id=rs.getInt(“pid”);
int pqty=rs.getInt(“pqty”);
整数价格=卢比整(“价格”);
%>
在这些更改之后,您必须在更改select时向servlet或另一个jsp发送一个ajax请求


在jsp或servet中,您为查找数量、价格和总额编写代码并返回此

,这将不起作用,之后我们会遇到相同的问题。请阅读我的完整答案如果您仅更改代码不起作用,您需要按id创建请求并获得响应