【问题标题】:jquery alertbox is getting disabled automaticallyjquery alertbox 被自动禁用
【发布时间】:2011-05-04 12:59:40
【问题描述】:

我已经把代码写在 ascx 脚本:

<script src="JScripts/jquery.alerts-1.1/jquery.alerts.js" type="text/javascript"></script>

<script>
$(function() {
        $('#ImageButton1').click(function() {
            jAlert('Please enter a valid Suggestion ID.', 'Case Entry');
        });
    });
</script>

然后继续 后面的代码:

Page.ClientScript.RegisterStartupScript(this.GetType(), "Window", "callAlert();", true);

问题是在页面完全加载一段时间后,警报框会自动禁用

单击确定按钮后警报框被禁用的原因以及如何正确调用 callAlert 函数。

【问题讨论】:

  • 您是否尝试在页面加载时显示警告框,并连接它以在用户单击ImageButton1 时显示?
  • 有一个文本框,当我在该文本框中输入一些内容时,我正在检查条件,如果条件为真,它将重定向到其他页面,否则将显示警报框。

标签: c# jquery asp.net


【解决方案1】:

如果您使用的是一个或多个母版页,那么您将不会获得按钮的客户端 ID,因为您声明它应该声明为 $('#&lt;%=ImageButton1.ClientID%&gt;')$('[id$=ImageButton1]') 希望它能解决您的问题。

$(document).ready(function(){
 $('#<%=ImageButton1.ClientID%>').click(function() {
            alert('Please enter a valid Suggestion ID.', 'Case Entry');
        });

});

【讨论】:

    【解决方案2】:

    你可以尝试在函数前面加上下面这行

    $(document).ready(function() {
    

    这样就可以了:

    $(document).ready(function() {
            $('#ImageButton1').click(function() {
                jAlert('Please enter a valid Suggestion ID.', 'Case Entry');
            });
        });
     });
    

    如果你等到页面准备好,警告框不会被覆盖(我希望 x)。

    此外,当您检查该文本框时,请检查条件是否为假,然后发出警报。
    条件不是假的吗?构建检查以检查条件是否真的为真。如果是这样?重定向。

    编辑:

    var answer = Confirm: ("This page will now redirect. Are you ready?")
    if (answer)
    //redirect
    else
    return
    

    【讨论】:

    • 我需要当警报框显示然后页面不应该重定向,直到有人点击 ok btn
    【解决方案3】:

    好的,首先重要的是要了解$(function(){...$(document).ready(function() {... 是等价的,在页面完全加载之前,其中的任何内容都不会执行。换句话说,没有必要使用

    Page.ClientScript.RegisterStartupScript(this.GetType(), "Window", "callAlert();", true);
    

    可以删除。另外,我看到您可能正在使用网络表单。请注意,将被渲染的Id属性不等于控件属性的Id。换句话说,如果您有一个 ID 为 ImageButton1runat="server" 控件,则在 jQuery 中使用语法 $('#ImageButton1') 将不起作用。

    考虑到这一点,我在下面添加了一个使用基于类属性的选择器的示例。

    <script type="text/javascript">
        $(function () {
            $('.ImageButton1').click(function (e) {
                var text = $('.TextBox1').val();
                var redirect = true;
    
                if (!text) {
                    redirect = confirm('Empty...are you sure?');
                }
    
                if (redirect) {
                    window.location.href = 'http://your-redirect-here.com';
                }
            });
        });
    </script>
    
    <input class="TextBox1" type="text" />
    <input class="ImageButton1" type="button" value="Click" />
    

    这应该可以带你去你想去的地方。如果您有任何问题,请告诉我。

    【讨论】:

      【解决方案4】:
      var answer = Confirm: ("This page will now redirect. Are you ready?")
      if (answer)
      {
         //redirect
      } else
      {
           return false;
       }
      

      【讨论】:

        【解决方案5】:

        把这个放在jAlert框之后:

        return false;
        

        然后像这样调用函数:

        return callAlert();
        

        【讨论】:

          猜你喜欢
          • 2020-08-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-08-21
          • 2021-07-22
          • 1970-01-01
          相关资源
          最近更新 更多