jquery ajax doesnt trigger in Internet Explorer

2019-08-24 03:59发布

I am using a function to trigger when any of a particular set of anchors are clicked.
This works in Firefox 3 and Chrome.

They don't exist when the page first starts loading as they are being created by XML+XSLT using client side transformation, just in case this is relevant some how.

I also only put that alert in there just to see if it was even doing anything... not even an alert in IE. It was not working in Chrome but that was because I was missing $document.ready statement [still worked in FF] but I am not sure what the missing element is for IE. I am using IE 8 btw.

        <script type="text/javascript">
        $(document).ready(function () {
            $("#BlogSelectList li a").click(function () {
                alert('hhi')
                var str = ($(this).attr("href")).slice(1, 37)
                $.ajax({
                    contentType: "application/json; charset=utf-8",
                    url: '../ws/WebServices.asmx/SetActiveBlog',
                    data: '{ActiveBlogID: "' + str + '"}',
                    dataType: 'json',
                    type: "post",
                    success: function (j) {
                            window.location.href = 'dashboard.aspx'
                    }

                });

            });
        })
    </script>

2条回答
姐就是有狂的资本
2楼-- · 2019-08-24 04:14

It probably is relevant that the anchors are not there when the doc first loads. You could do a quick fix by using the live method. This will apply your event to whatever you're selecting even if it's added to the DOM later.

$("#BlogSelectList li a").live("click", function() {etc...});

Also, if you're using an anchor, you will probably need to set the href attribute to "#" so that it doesn't trigger the browser default behavior of navigating somewhere.

查看更多
放我归山
3楼-- · 2019-08-24 04:18
    <script type="text/javascript" src="jquery-1.6.1.min.js"></script>
    <script type="text/javascript">
            $(document).ready(function () {
                $("#click").click(function () {
                    alert('hhi');
                    });
                    });

    </script>
<table>
<tr><td id='click'>ravi</td></tr>
</table>

try this in IE8 and tell me. once this code has not run due to popup blocker in IE8. remove popup blocker

查看更多
登录 后发表回答