2011-09-27 73 views
2

我試圖將控件的id傳遞給一個javascript函數,該函數將它的值(控件是一個文本框)添加到列表框中,但顯然我沒有理解它,是否有人請糾正我。將控制權作爲參數傳遞給javascript函數

謝謝。

<input type="button" ID="btnAddtoLstBox" value="" title="Add this to the list" onclick="javascript:addToList(document.getElementById(btnAddtoLstBox));" 
class="ui-icon ui-icon-refresh ui-corner-all" style="width: 20px; height: 20px; background-position: -64px 80px" /> 

    // scripts to add list items 
     function addToList(varTxtBox) { 

       // get the list box 
       var lb = document.getElementById("uilstMemTypeTier"); 

       // get the text to add 
       var toAdd = varTxtBox.value; 

       if (toAdd == "") return false; 

       // look for the delimiter string. if found, alert and do nothing 
       if (toAdd.indexOf(delim) != -1) { 
        alert("The value to add to the list cannot contain the text \"" + delim + "\" as it is used as the delimiter string."); 
        return false; 
       } 

       // check if the value is already in the list box 
       for (i = 0; i < lb.length; i++) { 
        if (toAdd == lb.options[i].value) { 
         alert("The text you tried to add is already in the list box."); 
         return false; 
        } 
       } 

       // add it to the hidden field 
       document.getElementById("<%=uihdnlistBasedFieldsListItems.ClientID%>").value += toAdd + delim; 

       // create an option and add it to the end of the listbox 
       lb.options[lb.length] = new Option(toAdd, toAdd); 

       // clear the textfield and focus it 
       varTxtBox.value = ""; 
       varTxtBox.focus(); 
      } 
+0

您是否收到錯誤?哪一位不工作? – ipr101

+0

另外,delim是否在封閉範圍內定義和定義? – alh84001

回答

6

變化onclick="javascript:addToList(document.getElementById(btnAddtoLstBox));"onclick="addToList(document.getElementById('btnAddtoLstBox'));"onclick="addToList(this);"

+0

感謝您的推動! – k80sg

+1

對於<%= id.ClientID%>呢? – 2014-05-13 04:55:34

0

你也可以以下面的方式去做 -

<body> 
<form id="form1" runat="server"> 
<div id="div1" style="height:100px; width:192px; background-color:red;"> 

</div> 
<br /> 
<div id="div2" style="height:100px; width:192px; background-color:green; display:block"> 

</div> 
<br /> 
    <asp:Button runat="server" Text="Change color" id="btnColor" OnClientClick="javascript:return changeColor();"/> 
    <asp:Button Text="Hide 1st" runat="server" ID="btnHide1st" OnClientClick="javascript:return hideDiv('div1');"/> 
    <asp:Button Text="Hide 2nd" runat="server" id="btnHide2nd" OnClientClick="javascript:return hideDiv('div2');"/> 
</form> 

希望這可以幫助你。

相關問題