未捕获的TypeError:undefined不是onClick函数

时间:2014-05-14 10:53:36

标签: javascript jsp

当我尝试点击关闭按钮时,我得到例外

Uncaught TypeError: undefined is not a function onClick

这是我的JSP代码段

<input name="close" id="closeIFrame" type="button" onClick="window.parent.location.href = window.parent.location.href; window.parent.closeSearchLightBox();" value='Close' title="Close" class="btn btn-primary pull-right" />

忘记提及closesearchLightBox定义为

    $(function() {
        $("#getAssistance").click(function(e) {
            e.preventDefault();
            var href = $(this).attr('href');
            if (href != undefined && href.indexOf('#') != 0 && href.indexOf('/broker/search') != 0) {
                $(
                        '<div id="searchBox" class="modal bigModal modalsize-l" data-backdrop="static"><div class="searchModal-header gutter10-lr"><button type="button" onclick="window.location.reload()" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="aria-hidden">close</span></button></div><div class=""><iframe id="search" src="' + href + '" class="searchModal-body"></iframe></div></div>')
                        .modal();
            }
        });
        $("#pop_findAgent").click(function(e) {
            e.preventDefault();
            var href = '/hix/broker/search?anonymousFlag=N';
            openFindBrokerDialog(href);
        });
    });

    function openFindBrokerDialog(href)
    {
        $('<div id="brokersearchBox" class="modal bigModal modalsize-l"><div class="searchModal-header"><button type="button" class="agentClose close"><span aria-hidden="true">&times;</span><span class="aria-hidden">close</span></button></div><div class=""><iframe id="search" src="' + href + '" class="searchModal-body"></iframe></div><div class="modal-footer txt-center"></div></div>').modal({show: true});
    }

    function closeSearchLightBox() {
        $("#searchBox").remove();
    }

    function closeSearchLightBoxOnCancel() {
        $("#searchBox").remove();
    }

    $('.agentClose').live('click',function(){
        $('#brokersearchBox, .modal-backdrop').remove();
        $('body').removeClass('modal-open');
        location.reload();          
    });

    $(document).keyup(function(e) {
        if (e.keyCode == 27) {
            $('#brokersearchBox').remove();
        }  
    });

    $('#closeAgentSerchBox').click(function(event){
        event.preventDefault();
        $( ".agentClose" ).trigger( "click" );
    });

2 个答案:

答案 0 :(得分:2)

将它放在单独的功能中并在点击时调用它:

<input name="close" id="closeIFrame" type="button" onClick="doIt()" value='Close' title="Close" class="btn btn-primary pull-right" />

function doIt() {
  window.parent.location.href = window.parent.location.href;
  window.parent.closeSearchLightBox();
}

是的,closeSearchLightBox()似乎未定义。

答案 1 :(得分:0)

方法window.parent.closeSearchLightBox()未定义。

如果您没有该方法,请添加以下代码

window.parent.closeSearchLightBox = function() {
   // your logic
}

但大多数情况下它会像window.closeSearchLightBox(),您可以从window.closeSearchLightBox

中删除父级
window.closeSearchLightBox = function() {
   // your logic
}

否则它可能就像下面一样,因为函数附加到窗口对象

function closeSearchLightBox() {
       // your logic
    }