无法在按钮单击时调用javascript函数

问题描述:





i无法在点击按钮上调用javascript函数。

i我收到以下错误



& gt; Microsoft JScript运行时错误:''AddTextBox''未定义





请找到以下代码:



资料来源:

hi ,

i am unable to call a javascript function on a button click .
i am getting the following error

> Microsoft JScript runtime error: ''AddTextBox'' is undefined


Please find the code below:

Source :

<asp:Button ID="Button2"  runat="server"   Text="Add License"
        onClientClick="AddTextBox()" onclick="Button2_Click1" />

       <div id="TextBoxContainer">
       <!----Textboxes will be added here--->
    </div>
    <script type="text/javascript">

        function GetDynamicTextBox(value) {

            return '<input name = "DynamicTextBox" type="text" value="' + value + '"/>' + '<input type="button" value="Remove" onclick="RemoveTextBox(this)"/>'
        }
         function AddTextBox(){
               var div = document.createElement('DIV');
               div.innerHTML = GetDynamicTextBox("");
               document.getElementById("TextBoxContainer").appendChild(div);
               }
        function RemoveTextBox(div) {
            document.getElementById("TextBoxContainer").removeChild(div.parentNode);
        }
        function RecreateDynamicTextBox(){
            var values=eval('=Values');
            if(values!=null){
              var html="";
              for(var i=0;i<values.length;i++){>
                 html+="<div>" + GetDynamicTextBox*(values[i] + "<div>";
                 }
                 document.getElementById("TextBoxContainer").innerHTML=html;
                 }
             }
             window.onload=RecreateDynamicTextBoxes;

    </script>





按钮代码点击:



Code of button click:

protected void Button2_Click(object sender, EventArgs e)
{
    string[] textboxvalues = Request.Form.GetValues("DynamicTextBox");
    JavaScriptSerializer serializer = new JavaScriptSerializer();
    this.Values = serializer.Serialize(textboxvalues);
}



请提前帮助和谢谢....



谢谢


Please help and thanks in advance....

Thanks

这是一个语法错误。因为你在这里错过了一个紧密的大括号''''':

It''s a syntacticle error. Since you missed a close braces '')'' here:
html+="<div>" + GetDynamicTextBox*(values[i] + "<div>";



用以下内容替换该行:


Replace the line with following:

html+="<div>" + GetDynamicTextBox*(values[i] + "<div>");



还有onload你打电话 window.onload = RecreateDynamicTextBoxes; 而你的函数名是 RecreateDynamicTextBox



--Amit


And also onload you are calling window.onload=RecreateDynamicTextBoxes; whereas your function name is RecreateDynamicTextBox

--Amit


我认为,在onClick事件中你调用了
I think, in onClick event you have called
onclick="Button2_Click1"



但是没有相同的定义。



所以通过将onClick事件更改为


but there is no definition for the same.

So by changing the onClick event to

onclick="Button2_Click"

可能适合你。