【问题标题】:Javascript works in Chrome, but not in IE or FirefoxJavascript 在 Chrome 中有效,但在 IE 或 Firefox 中无效
【发布时间】:2012-04-30 15:33:35
【问题描述】:

When one or fewer checkboxes are selected and you click the Compare tours button, a popup should appear.但是,此功能仅适用于 Chrome。我该怎么做才能让它在 Firefox 和 IE 中运行?

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder" runat="Server">
<script type="text/javascript">
    $(document).ready(function () {
        var selectedCheckboxes = $("input:checkbox[id$=Select]");
        $("[id*=CompareTop]").click(function() {
            if (selectedCheckboxes.filter(":checked").length <= 1) {
                event.preventDefault();
                alert("Please select at least 2 tours to compare.");}
        });            
        selectedCheckboxes.change(function() {
            if (selectedCheckboxes.filter(":checked").length > 3){
                $(this).removeAttr("checked");
                alert("At most 3 tours may be compared.");  
            }
        });
    });

          </script>
<sc:Placeholder ID="phHeader" Key="ph_header" runat="server" />

【问题讨论】:

  • 我先添加“});”在脚本的末尾。
  • 它正在关闭,我剪掉了一些正常运行的 j/s,然后 $(document).ready 被关闭了 - 我只是忘了把它包括在这里。
  • 你试过在firefox中打开Firebug运行它吗?这将有助于调试问题。
  • 是的——在朋友的帮助下,我们决定可能需要将“事件”作为参数传递——一旦我得出结论,如果这是解决方案,我会更新它。
  • 你的朋友是对的。就目前而言,event.preventDefault() 不起作用,因为函数中没有名为 event 的变量(至少在我们所见的范围内)。

标签: javascript asp.net internet-explorer firefox google-chrome


【解决方案1】:

你的函数应该接受一个名为“event”的参数,不是吗?事实上,它确实不能在 Firefox 中运行。

【讨论】:

    【解决方案2】:

    你少了一个 });在末尾。你需要关闭 $(document).ready

    【讨论】:

    • 对不起,我实际上排除了更多运行正常的 j/s 代码,并且 $(document).ready 在那里关闭。
    猜你喜欢
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多