防止在禁用单选按钮列表上调用 javascript 函数

Prevent calling javascript function on disable radiobuttonlist

本文关键字:调用 javascript 函数 列表 单选按钮      更新时间:2023-09-26

Code

<asp:RadioButtonList ID="rdStatus" onclick="javacript:display();" runat="server" RepeatDirection="Vertical">
<asp:ListItem Text="Temporary Waiver" Value="T" Selected="True"></asp:ListItem>    
<asp:ListItem Text="Never Expires" Value="P"></asp:ListItem>    
<asp:ListItem Text="Expires after the close of:" Value="W"></asp:ListItem>
</asp:RadioButtonList>

我的问题:

  • 在运行期间,将为单选按钮列表创建此代码 HTML 表,并将单击事件放置在表上。
  • 即使我禁用单选按钮列表,也会发生点击事件。我需要避免这个问题.请提出一些想法。

在禁用单选按钮上将类添加为"禁用"到单选按钮和更好的方法来使用 jQuery 而不是 javascript 作为波纹管

jQuery("#rdStatus").click(function()`{  
    if(jQuery(this).hasClass("disabled"))
        return false;
    continue with you code here.............
}`);

您编写的代码完全按照您的要求执行:当用户单击元素时,函数将运行。有两种方法可以更改它以获得所需的效果:

  1. 使用其他事件而不是点击
  2. 停止函数执行

另一个事件

您可能希望查看 CHANGE 事件 (https://developer.mozilla.org/en-US/docs/Web/Reference/Events/change)

停止功能

在函数中,您可以检查单选按钮列表的状态。如果禁用它,则只需从函数return;,而不是运行实际有用的代码。如果您不想更改原始函数,则可以编写一个小包装器,该包装器仅在启用 RadioButtonList 时才调用它,并将包装器用作"click"事件的处理程序。