確定 UI 對應的唯一元素 ID

下列描述如何修改 JavaScript 程式碼,確定適當地轉譯 UI 對應的唯一元素 ID。

只有針對下列程式碼才需要進行修改:使用 getElementById() (或類似) 函數來轉譯 HTML,以產生列表 ID 以及避免帳戶確認或相關錯誤。

下列範本程式碼片段包含所需的修改:


...
function getElementsFromList(namePrefix) {
    var ret = [];
    var elements = document.getElementsByTagName("INPUT");
    for(var i=0;i<elements.length;i++) {
        var elemID = elements[i].id;
        if((id) && (id.startsWith(namePrefix + '_')) {
            ret.push(elements[i]);
        }
    }
...
return ret;

由於 ID 在產生的 UI 對應 ID 中不一定是唯一的,上面顯示的程式碼會附加底線和列號 (例如,myField_1、myField_2),藉此確保執行時期的唯一性,讓架構能在轉譯的 HTML 中進行適當處理,同時還能讓您參考產生的 UI 對應中包含的未修改 ID。

在 spl.properties 檔案中有一個開關也可以讓您停用針對方格中的元素產生唯一 ID 的功能 (如下所述),但是為了符合標準規範,強烈建議此開關保留預設值。

 
Property Name: spl.runtime.compatibility.uiMapDisableGenerateUniqueHtmlIDs
File Name: spl.properties (under web project in FW)
Default Value: false
Accepted Values: true or false
Description: This property controls the generation of unique IDs for all input elements inside 
a list. When this value is set to true it disables the generation of unique IDs, thus 
replicating the old behavior. When this property is set to false or this property is missing 
it enables the generation of unique IDs, thus enabling the list to be standards-compliant.