我想在文本框中显示下拉列表中所选项目的数量

时间:2014-03-17 17:47:02

标签: jsp servlets

<script type="text/javascript">

function showconfirm() {
    var a = confirm("Do you want to Buy ?");    
    if(a == true) {
        alert("Book is Yours!");
    } else {
        alert("Paisa nahi hai toh aage bado!!");
    }
}
</script>

我的问题是数据库中最后一项的价格和数量是否已填入文本框。情况并非如此。每次选择新项目时,数量和价格都应该改变。

<table align = 'center' cellspacing='15' style = "background-color:brown" >
    <tr>    

      <%
       try {
          data = new MysqlDataSource();
          con =  data.getConnection("root","system");
          System.out.println("Connected to MySQL"); 
          PreparedStatement pre = con.prepareStatement("select * from library.booklist");
          result = pre.executeQuery();       
      %> 

        <td> <i> Book List : </i></td> 
         <td> <select name= "BookList" id="Booklist" > 
              <option> - select - </option>
    <%  
    while (result.next())  
    {
           name = result.getString("Book"); 

    %>
    <option value="<%=name%>"> <%=name%></option>

    <% 
    }
}
catch (SQLException e1) {
    e1.printStackTrace();
}
    %>
</select>
</td>
</tr>

    <tr>
       <%
    try {
     ItemNames [] = request.getParameterValues("BookList");
     System.out.println(ItemNames);
     pre = con.prepareStatement("select Quantity from library.booklist where Book = ? ");  
     pre.setString(1,ItemNames); 
      result = pre.executeQuery();
      %>
     <td><i> Quantity : </i>  </td>
  <%
    while (result.next())  
    {
       int show = result.getInt("Quantity"); 
  %>
<td> <input name = "Quantity" id = "Quantity" type = "text" size = "10" readonly = "readonly" value = "<%=show%>"> </td>
   <%
   }
   } 
    catch (SQLException e1) 
    {
    e1.printStackTrace();
    }

   %>
   </tr>



     <tr> 
    <%
    try {
     pre = con.prepareStatement("select Price from library.booklist where Book = ? ");  
     pre.setString(1,name); 
      result = pre.executeQuery();
      %>

      <td><i> Price : </i>  </td> 
   <%
    while (result.next())  
    {
       double mrp = result.getDouble("Price"); 
   %>

<td> <input name = "Price" id = "Price" type = "text" size = "10" readonly = "readonly" value = "<%=mrp%>">  </td>
   <%
   }
   } 
    catch (SQLException e1) 
    {
    e1.printStackTrace();
    }  
   %>
     </tr> 

    <tr>
    <td> </td>
    <td><input label='Submit' value='Buy' type='Submit'  onclick = 'showconfirm()' value ='Showconfirmbox' /> </td>
    </tr>
</table>   

我想在下拉列表中选择任何项目时将价格和数量显示在文本框中。数据存储在数据库中。如何从jsp列表中选择数据?

1 个答案:

答案 0 :(得分:0)

  

每次选择新商品时,数量和价格都会发生变化。

Javascript和Ajax。如果您希望在用户在界面上执行某些操作时发生某些事情,则必须使用Javascript。因为只有Javascript才能响应客户端事件。

  

如何从jsp中的列表中选择数据?

在此代码中,您构建要发送给客户端的页面。显然,由于客户尚未看到该页面,他们还没有选择任何内容。