0
我在我的Javascript函數中動態創建了一個按鈕,並調用了另一個函數onclick,如下所示。動態創建的表單通過onclick調用立即消失Javascript
streambtn = document.createElement("button");
streambtn.setAttribute("onclick","createattribute()");
createattribute()方法在動態創建的表單中打開一個細分,該函數如下所示。
function createattribute() {
inputval.innerHTML = "Provide a Stream name and click to add attribute-type pairs to yours stream.";
var input = document.createElement("input");
input.type = "text";
input.placeholder="Attribute name";
input.id="attr"+attrID;
input.className = "attr-textfield-style";
inputval.appendChild(input);
//Display the drop down menu
var newDiv=document.createElement('div');
var html = '<select>', attrtypes = typeGenerate(), i;
for(i = 0; i < attrtypes.length; i++) {
html += "<option value='"+attrtypes[i]+"'>"+attrtypes[i]+"</option>";
}
html += '</select>';
newDiv.className="attr-combobox-style";
newDiv.id="type"+attrID;
newDiv.innerHTML= html;
inputval.appendChild(newDiv);
attrID++;
alert("attrname id: "+input.id+" attrtype id: "+ newDiv.id);
}
inputval是動態創建表單中的一個區域,createattribute()函數中新創建的表單元素將被附加到該表單中。但是我現在面臨的問題是,儘管正確調用了該方法,但一旦單擊streambtn,表單(動態創建的表單)立即消失。點擊後,它會顯示第一行「提供一個流名稱,然後單擊以將屬性類型對添加到您的流中。」在一瞬間消失。有沒有辦法將它保留在屏幕上,然後根據任何條件隱藏它(例如在用戶輸入與新打開的表單相對應的數據後 - >通過createattribute()方法創建)?
非常感謝。這工作。雖然我確實知道表單是通過單擊按鈕提交的,但我不知道我必須單獨聲明類型,因爲它是在createElement(「button」)行中指定的。 –
@NayantaraWilfred即使它是按鈕標記,您也可以定義類型以防止點擊事件中的表單提交... – Rayon