无法取消按钮单击

时间:2022-07-01 21:05:29

I have a html button in a web page. When I click the button, I'd like to redirect to another page.

我在网页上有一个html按钮。当我点击按钮时,我想重定向到另一个页面。

I have this code :

我有这个代码:

<button id="btn" type="submit" style="width: auto">
    <div style="height: 40px">
        <img alt="Button" src="/images/somepicture.png" style="float: left" />
        <span style="margin: auto">Label</span>
    </div>
</button>
<script language="javascript" type="text/javascript">
    $(function () {
        $("#btn")
        .mousedown(function (event) {
            window.location.href = "otherpage";
            event.preventDefault();
        });
    });
</script>

However, this code is not behaving as expected. The page is simply posting, and the redirection does not occurs.

但是,此代码的行为不符合预期。该页面只是发布,并且不会发生重定向。

What is wrong with my code?

我的代码出了什么问题?

2 个解决方案

#1


3  

This should work for you:

这应该适合你:

$(function () {
    $("#btn").click(function (event) {
        event.preventDefault();
        window.location.href = "otherpage";
    });
});

Note: Text and images are the only valid contents for a <button>. I don't think this would be the cause of your issue, but it is best to follow the standards.

注意:文本和图像是

#2


2  

note that it's not valid to use div tags within button tags, try this:

请注意,在按钮标记中使用div标签无效,请尝试以下操作:

$(function() {
    $("#btn").click(function(event) { // you can use click handler
        window.location = "http://www.*.com";
        event.preventDefault();
    });
});

#1


3  

This should work for you:

这应该适合你:

$(function () {
    $("#btn").click(function (event) {
        event.preventDefault();
        window.location.href = "otherpage";
    });
});

Note: Text and images are the only valid contents for a <button>. I don't think this would be the cause of your issue, but it is best to follow the standards.

注意:文本和图像是

#2


2  

note that it's not valid to use div tags within button tags, try this:

请注意,在按钮标记中使用div标签无效,请尝试以下操作:

$(function() {
    $("#btn").click(function(event) { // you can use click handler
        window.location = "http://www.*.com";
        event.preventDefault();
    });
});