簡體   English   中英

如何獲取將存儲在表單的select選項值中的數據?

[英]How to fetch data that will be stored in the select option value in the form?

我想從數據庫中動態獲取或獲取數據並填充選擇框。 這是我的代碼:

    <form action="addchangerequest" method="post">
        <select name="type">
            <option value="Non-Sap">Non-Sap</option>
            <option value="Sap">Sap</option>
        </select>

        <select name="assettriggered">
            ** I want to populate these option values upon making a selection to the first select box. These option will be the data coming from the database**
        </select>


    </form>

這是我的方法類。

    public List sortDeployed(String userID)
{
    List deployedlist = new ArrayList();

    try
    {

        PreparedStatement pstmt = conn.prepareStatement(SORT_ID);
        pstmt.setString(1, userID);
        ResultSet rs = pstmt.executeQuery();

        while(rs.next())
        {
            StoreAssetBean storeAssetBean = new StoreAssetBean();
            storeAssetBean.setDeployedID(rs.getInt("AssetDeployed_ID"));
            storeAssetBean.setName(rs.getString("Asset_Name"));
            storeAssetBean.setMmoID(rs.getString("UserDivision_ID"));
            storeAssetBean.setDepartment(rs.getString("User_Department"));
            storeAssetBean.setDepType(rs.getString("AssetDeployed_Type"));
            storeAssetBean.setDeployedDateTime(rs.getString("AssetDeployed_DateTime"));
            deployedlist.add(storeAssetBean);
        }

    }
    catch(SQLException e)
    {
        System.out.println(e);
    }
    return deployedlist;
}

我希望必須將AssetDeployed_Type數據導入名為“ assettriggered”的選擇框。 我正在使用MVC Model 2 JSP Servlet。 我已經搜索過有關ajax的信息,但是我沒有實現它的經驗。 當我在第一個選擇框中選擇值時,我希望它是動態數據獲取。 請幫助我,謝謝你!

當您了解需要采取的步驟時,這很容易做到。

  1. 在您需要的某些JavaScript事件上發送AJAX請求。 在您的情況下,它是第一個<select> onchange 當然,您可以自己發送XMLHttpRequest ,但是通過使用成熟的JS庫(如jQuery),這樣做更容易(且更穩定)。
  2. 該請求由專用的Servlet接收,該Servlet接受一些數據,這些數據將用於獲取Java代碼中的必要數據,並將獲取的JSON格式的數據發送回Web瀏覽器。 如果您打算將此servlet用於其他目的,則輸入的數據是第一個<select>元素的所選<option>的值,並且可能是獲取操作的id。 從servlet返回的數據以JSON格式序列化可以通過使用類似Gson的庫來完成。
  3. 當servlet響應成功返回到客戶端時,將使用其中的servlet數據執行JS回調函數。 您將使用該數據來創建HTML元素,使用特定於應用程序的數據填充它們,並使用新獲取的數據更新HTML DOM(第二個<select>元素)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM