jQuery表单提交事件只适用于Chrome浏览器

jQuery form Submit event only works in Chrome

本文关键字:Chrome 浏览器 适用于 表单提交 事件 jQuery      更新时间:2023-09-26

我的代码:

$("#addhash").submit(function(event) {
        // Cancel the default action
        event.preventDefault();
        // Store the value and remove any white space or disallowed chars
        var hash = $("#hashtoadd").val().replace(/'s/g, "").replace(/'W/g, "");
        // Check if anything was entered
        if(hash) {
            // Add the hashtag to the array
            hashes.push(hash);
            localStorage.hashes = JSON.stringify(hashes);
            var key = hashes.length - 1;
            // Clear the form field
            $("#hashtoadd").val("");
            // Set others to inactive
            $("#hashes").children().removeClass("active");
            // Add it to the list and make it the active page
            $("#hashes").append("<li class='active' id='" + key + "'><a href='#' class='close'>&times;</a><a class='hashtag' href='#" + hash + "'>#" + hash + "</a></li>");
            // Load the tweets
            getTweets(hash);
        }
});

此代码将只在Chrome中工作。看起来好像event.preventDefault();甚至没有开火。我试着在一开始就放一个警告对话框,但它也没有显示出来,这让我相信它根本就没有进入这里。我试过只使用click ()事件,也只使用submit()事件,但这些都不起作用。

EDIT:我认为问题是事件处理程序从未在其他浏览器中被调用。我已经更新了代码,以反映它目前的情况。

EDIT2:按要求,这是表单本身:

<form class="navbar-form pull-right" id="addhash">
    <div class="input-prepend input-append">
        <span class="add-on">#</span>
        <input type="text" placeholder="tag" tabindex="1" id="hashtoadd">
        <button class="btn" type="submit" href="#" id="submit"><i class="icon-plus"></i></button>
    </div>
</form>

不确定这是否是您的问题,但是

"从jQuery 1.7开始,.live()方法已被弃用。使用.on()来附加事件处理程序。"

。live只需要在选择的对象/s在DOM中不存在时使用,其他情况下你可以使用。bind()或在你的表单的情况下。submit()