开发者

JS variable undefined in onclick event in IE

I have a small f开发者_JS百科orm on my page where a user can either search for availability at a specific hotel, or search for availability at all hotels:

<script type="text/javascript" language="javascript">
    function Redirect() {
        if (searchAll) {
            document.getElementById("Hotel").value = "0";
        }

        return true;
    }
</script>

<form name="form1" action="search.asp" method="POST" id="form1"
    onsubmit="return Redirect()">

    <input type="hidden" id="Hotel" name="Hotel" value="<%= HotelID %>">

    <input id="btnHotelSearch" type="submit" name="btnHotelSearch"
        value="Search This Hotel" onclick="searchAll=false">
    <input id="btnHotelSearchAll" type="submit" name="btnHotelSearchAll"
        value="Search All Hotels" onclick="searchAll=true">
</form>

Whenever a button is pushed, I run some javascript to set the HotelID being passed. This works fine in Firefox. But in IE, whenever I push either button, I get a script error saying 'searchAll' is undefined

What do I need to do to fix that?


You need to declare the searchAll variable outside of the Redirect function, so it's in the global scope and therefore accessible from the Redirect function, and your inline event handlers:

var searchAll;
function Redirect() {
    if (searchAll) {
        document.getElementById("Hotel").value = "0";
    }

    return true;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜