【问题标题】:Set Color in DataBase when specific button clicked单击特定按钮时在数据库中设置颜色
【发布时间】:2016-10-31 05:52:55
【问题描述】:

我有两个按钮:一个在点击时设置颜色为红色,另一个设置为绿色。
我想将该值存储在单击哪个按钮的数据库中。
之后,我想从数据库中获取此颜色,并希望使用<td bgcolor='' align="center" > 标签中的 get 属性在背景标签中设置。 我怎样才能轻松做到?

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
         </br>
        <h3> Please select your location </h3>
            <table border="1" width="50%" height="500" align="center"  >
            <tbody>
                <tr>           
                    <td bgcolor='' align="center" >
                        <jsp:useBean id="bean" scope="session" class="org.functions" />
                        <input type="submit" value="Park Here" color='<jsp:setProperty name="bean" property="color" value="red" />' name="en" onclick="this.parentNode.style.backgroundColor='red'" />
                        <input type="submit" value="Cancel" color='<jsp:setProperty name="bean" property="color" value="green" />' name="en" onclick="this.parentNode.style.backgroundColor='green'" />

                    </td>
<tr>
                    <form action="hello.jsp">
                    <td colspan="2" align="center">
                        <input type="submit" value="Ok" style="width:100px"  />
                    </td>
                    </form>
                </tr>

            </tbody>
        </table>                     
    </body>
</html>

<%-- 
    Document   : hello
    Created on : Jun 19, 2016, 5:49:58 PM
    Author     : 
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1 align="center">This is hello.jsp </h1>
        <jsp:useBean id="mybean" scope="session" class="org.Second" />

        <jsp:setProperty name="mybean" property="name" />
        <jsp:setProperty name="mybean" property="mail" />
        <jsp:useBean id="bean" scope="session" class="org.functions" />

        <%
                   // Second scnd = new Second();
                       int a= mybean.storevalue(); 
                       int b= bean.storeColor();                               
                 %>
                  <jsp:setProperty name="bean" property="color"  />
                  <jsp:setProperty name="bean" property="id"  />
        <table border="1" align="center">
            <thead>  
            <tr>
                <td>From Values</td>
            </tr>
            </thead>
        </table>
        <form action="ExampleDate.jsp" method="get">

            Start Date 
            <input type="date" name="date" id="d"  >

            Start Date 
            <input type="time" name="time"  >       
             <br></br>
        <input type="submit" value="hmmm" name="vxc" />
        </form>      
    </body>           
</html>

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 *
 * @author 
 */
public class functions {
    private String color;
    private String id;

    /**
     * @return the color
     */

    public String getColor() {
        return color;
    }

    /**
     * @param color the color to set
     */
    public void setColor(String color) {
        this.color = color;
    }

    /**
     * @return the id
     */
    public String getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(String id) {
        this.id = id;
    }

    public int storeColor() throws ClassNotFoundException, SQLException{
        Class.forName("org.apache.derby.jdbc.ClientDriver");
        String url = "jdbc:derby://localhost:1527/sample;create=true; user=app; password=app";
        Connection con = DriverManager.getConnection(url);
        PreparedStatement ps = con.prepareStatement("INSERT INTO COLORTABLE(en, id) VALUES(?, ?)");
        ps.setString(1, color);
        ps.setString(2, id);
        int a = ps.executeUpdate();
        if(a==1){
            return a;
        }else{
            return a;
        }
    }        
}

【问题讨论】:

  • 点击按钮触发java方法调用,因为jsp bean会有值,你可以使用那个bean属性将值发送到DAO
  • 我是初学者,我想使用一些特定的功能,但我不知道该怎么做。

标签: javascript java html sql


【解决方案1】:

你需要在前端这样做

在后端您只能存储颜色的值

【讨论】:

  • 你能解释一下吗?我该怎么做?
【解决方案2】:

一个简单的方法是使用 jQuery。您可以为此使用 addClass() 例程。如果您想恢复上一个按钮的初始值,您也可以实现该功能。例如:

//Restore all values
$("input").removeClass();
$("input").addClass("default");

//Set class that has the highlight color
$("#button2").addClass("highlight");

使用此 HTML:

<input type="button" id="button1"/>
<input type="button" id="button2"/>

还有这个 CSS:

.default { background-color: blue;}
.highlight { background-color: yellow;}

将 onClick 添加到元素以调用正确的函数。

【讨论】:

    【解决方案3】:

    所有的事情都是用稍微改变的逻辑完成的,但是现在我希望当“Park Here”点击它时,外部区域应该是红色的,当“Cancel”点击它应该是绿色的,但它显示内部颜色为红色和绿色。是否有任何方法可以将背景颜色更改为红色和绿色。 代码在这里

    <tbody>
      <tr>
           <td bgcolor="green" align="center" ><select name="place1"  >
           <option onclick="this.parentNode.style.backgroundColor='red' "> Park Here</option>
           <option onclick="this.parentNode.style.backgroundColor='green' "> Cancel </option>
           </select></td>
    
      </tr>
      </tbody>
    

    这是输出,但我希望外部颜色应该是红色或绿色,就像这个标签所做的那样。 有什么方法吗?请告诉我 I want outer color should be red on click not inner

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-06
      • 2020-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-14
      • 2020-11-27
      相关资源
      最近更新 更多