Javascript OnChange on textbox事件调用两次

Javascript OnChange on textbox事件调用两次,javascript,jquery,asp.net,Javascript,Jquery,Asp.net,我正在使用jQuery动态添加onchange事件。当我更改文本框时,事件会触发两次,两次警报框有时会出现三次 if(Country.toUpperCase().indexOf("MALAYSIA")!=-1) { debugger; if(productDesc.toUpperCase().indexOf("SV")!=-1) { $("#<%=txtlAxis.ClientID%>").change(fu

我正在使用jQuery动态添加onchange事件。当我更改文本框时,事件会触发两次,两次警报框有时会出现三次

if(Country.toUpperCase().indexOf("MALAYSIA")!=-1)
{    debugger;
    if(productDesc.toUpperCase().indexOf("SV")!=-1)
    {                   
        $("#<%=txtlAxis.ClientID%>").change(function()
        {         
                if(productDesc.toUpperCase().indexOf("SV")!=-1)
                {                           
                alert('2');
                }
        });  
    }
}
if(Country.toUpperCase().indexOf(“马来西亚”)!=-1)
{调试器;
if(productDesc.toUpperCase().indexOf(“SV”)!=-1)
{                   
$(“#”)更改(函数()
{         
if(productDesc.toUpperCase().indexOf(“SV”)!=-1)
{                           
警报(“2”);
}
});  
}
}

在此代码中是否可以使用解除绑定

if(Country.toUpperCase().indexOf("MALAYSIA")!=-1)
   {  
        if(productDesc.toUpperCase().indexOf("SV")!=-1)
        {       
            $("#<%=txtlAxis.ClientID%>").unbind();
            $("#<%=txtlAxis.ClientID%>").change(function()
            {         
                 if(productDesc.toUpperCase().indexOf("SV")!=-1)
                 {                           
                    alert('2');
                 }
            });  
        }
   }
if(Country.toUpperCase().indexOf(“马来西亚”)!=-1)
{  
if(productDesc.toUpperCase().indexOf(“SV”)!=-1)
{       
$(“#”).unbind();
$(“#”)更改(函数()
{         
if(productDesc.toUpperCase().indexOf(“SV”)!=-1)
{                           
警报(“2”);
}
});  
}
}

试试下面的方法
Unbind
OnChange事件,并动态地再次添加它

if(Country.toUpperCase().indexOf("MALAYSIA")!=-1)
{    
    if(productDesc.toUpperCase().indexOf("SV")!=-1)
    {      
        $('.txtlAxisClass').attr("onchange", "");             
        $("#<%=txtlAxis.ClientID%>").change(function()
        {         
             if(productDesc.toUpperCase().indexOf("SV")!=-1)
             {                           
                alert('2');
             }
        });  
    }
}
if(Country.toUpperCase().indexOf(“马来西亚”)!=-1)
{    
if(productDesc.toUpperCase().indexOf(“SV”)!=-1)
{      
$('.txtlAxisClass').attr(“onchange”,”);
$(“#”)更改(函数()
{         
if(productDesc.toUpperCase().indexOf(“SV”)!=-1)
{                           
警报(“2”);
}
});  
}
}
您还可以通过重新计算下面提到的表达式来降低性能。所以把它保存在变量中


productDesc.toUpperCase().indexOf(“SV”)
其固定值。非常感谢您的回复。MMK unbind()是正确的。我也使用了Kanavi的.attr(“onchange”,但没有任何用处。签入IE8和CROME

是的,Kanavi,我将把它转换成一个变量thx,以供建议。堆垛溢出的岩石

       if(Country.toUpperCase().indexOf("MALAYSIA")!=-1)
       {    
            if(productDesc.toUpperCase().indexOf("SV")!=-1)
            {                  
                $(".clstxtlAxis").unbind("change"); 
                $(".clstxtlAxis").change(function()
                {             
                     if(productDesc.toUpperCase().indexOf("SV")!=-1)
                     {
                        alert("2");                                                   
                     }
                }); 
             }
        } 

动态添加onchange事件
也许你绑定了好几次处理程序,并且所有的处理程序都在执行中,添加更多相关的代码。这看起来像是js和c的混合体-id为“”的元素是什么?需要更多的代码在你的problemJoy和leonxki中分区-它很好地实现了功能(我在调试模式中看到过)。。但是2次$(“#”)此代码没有问题。您的代码有什么不同?(如果你在你的答案中添加一条关于你添加了什么以及为什么添加的注释会更有帮助)嗨,MMK和Kanavi,所有这些,都是固定的。非常感谢您的回复。MMK unbind()是正确的。我也使用了Kanavi的.attr(“onchange”,但没有任何用处。签入IE8和CROME。是的,Kanavi,我将把它转换成一个变量thx,以供建议。StackOverflow ROCKS.Kannavi/MMK,为什么.attr(“onchange”)不起作用?