2014-02-19 31 views
1

我不知道如何使用下拉菜單外的點擊按鈕(可以是加號),而不是點擊「其他」,以便用戶可以輸入另一個值?謝謝!!如何在菜單外添加單擊按鈕以打開新的文本字段而不是單擊「其他」?

下面是HTML:

<form onsubmit="FormSubmit(this);"> 
<label for="last_name">Brand:</label> 
<input type="hidden" name="brand" /> 
<select name="fruit_ddl" onchange="DropDownChanged(this);"> 
<option value="">--select--</option> 
<option value="brand1">Brand1</option> 
<option value="brand2">Brand2 </option> 
<option value="brand3">Brand3</option> 
<option value="">Other..</option> 

</select> <input type="text" name="brand_txt" style="display: none;" /> 
<button type="submit">Add Brand</button> 
</form> 

的javascript:

function DropDownChanged(oDDL) { 
    var oTextbox = oDDL.form.elements["brand_txt"]; 
    if (oTextbox) { 
     oTextbox.style.display = (oDDL.value == "") ? "" : "none"; 
     if (oDDL.value == "") 
      oTextbox.focus(); 
    } 
} 

function FormSubmit(oForm) { 
    var oHidden = oForm.elements["brand"]; 
    var oDDL = oForm.elements["brand_ddl"]; 
    var oTextbox = oForm.elements["brand_txt"]; 

    if (oHidden && oDDL && oTextbox) 
     oHidden.value = (oDDL.value == "") ? oTextbox.value : oDDL.value; 
} 
+0

您希望能夠動態地將'

+0

感謝您的評論。是的,我想通過在菜單外添加一個具有相同功能的點擊按鈕來切換下拉菜單中的「其他」 –

回答

1

this fiddle你想要做什麼? 我保留了「其他」選項,而加號按鈕只是選擇了它。 (我不得不恰克「其他」的價值,因爲它是一樣的空值)

我基本上都添加了一個加號按鈕與此onclick處理程序:

function PlusClick(btn) { 
    var ddl = btn.form.elements["fruit_ddl"]; 
    ddl.value = "-";  
    DropDownChanged(ddl); 
} 

編輯:這是another fiddle ,刪除了「其他」選項。

相關問題