Javascript函数只执行一行

标签 javascript jquery

我有一个 div,一开始是隐藏的,单击按钮时会显示。 div上还有一个按钮,onclick事件调用这个函数:

    function popuppage2OK() {
        alert("you clicked ok!");
        var x = new Object();
        x.name = $("#boxforname").val();    //this is a text input
        x.option = $("#boxforoption").val();//this is a text input
        alert("hiding newfactorpage2");
        $("#popupform").hide();             //this is a div containing the text inputs and the button with the onlcick event that calls this function
        alert("popupform hidden");
        displaystuff();                     //another function ive written that needs to be called
        alert("this is after the display attempt");
    }

我的问题是,唯一执行的行似乎是隐藏 div 的行。没有任何警告框出现,并且 Displaystuff() 函数不会被执行,但 div 会恢复隐藏状态。关于为什么代码行可能会像这样被跳过的任何想法?

最佳答案

什么时候将事件处理程序附加到 div 内的按钮?

您应该在页面加载完成后执行此操作,因此在 Jquery 中您可以执行以下操作:

$(document).ready(function () {
    //attach the eventhandler here 
})

关于Javascript函数只执行一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6848964/

相关文章:

javascript - 深色模式开关

javascript - 如何防止移动浏览器中的模拟鼠标事件?

javascript - 流体动态 CSS - Javascript/jQuery

jQuery 验证单个元素上的 errorPlacement

javascript - 如果存在 2 个子 div,隐藏父 div?

javascript - jQuery on change 仅显示所选选项,删除/禁用其余选项

javascript - 使用 jQuery 菜单播放 "peek-a-boo"

JavaScript 数组有元素但长度为零

javascript - 检测 Ajax 调用内 div 的点击

javascript - NVD3 加载时重绘图表 'show'