How to create a HTML Cancel button that redirects to a URL

前端 未结 11 1143
Happy的楠姐
Happy的楠姐 2020-12-13 13:00

I am playing with the Buttons in the w3schools Tryit editor, and I am trying to figure out how to make my browser redirect to an URL when I click on the \"Cancel\" button.

相关标签:
11条回答
  • 2020-12-13 13:58

    Thats what i am using try it.

    <a href="index.php"><button style ="position:absolute;top:450px;left:1100px;height:30px;width:200px;"> Cancel </button></a>
    
    0 讨论(0)
  • 2020-12-13 13:59
    <input class="button" type="button" onclick="window.location.replace('your_url')" value="Cancel" />
    
    0 讨论(0)
  • 2020-12-13 14:00

    There are a few problems here.

    First of all, there is no such thing as <button type="cancel">, so it is treated as just a <button>. This means that your form will be submitted, instead of the button taking you elsewhere.

    Second, javascript: is only needed in href or action attributes, where a URL is expected, to designate JavaScript code. Inside onclick, where JavaScript is already expected, it merely acts as a label and serves no real purpose.

    Finally, it's just generally better design to have a cancel link rather than a cancel button. So you can just do this:

    <a href="http://stackoverflow.com/">Cancel</a>
    

    With CSS you can even make it look the same as a button, but with this HTML there is absolutely no confusion as to what it is supposed to do.

    0 讨论(0)
  • 2020-12-13 14:01

    Just put type="button"

    <button type="button"><b>Cancel</b></button>
    

    Because your button is inside a form it is taking default value as submit and type="cancel" doesn't exist.

    0 讨论(0)
  • 2020-12-13 14:04
    <button onclick=\"window.location='{$_SERVER['PHP_SELF']}';return false;\">Reset</button>
    

    Not enough rep to Vote Up for Kostyan. Here's my final solution (needed a reset button).

    Thanks again to Kostyan for answering the question as asked without suggesting a "workaround" (time-consuming) method to "construct a button" with styles.

    This is a Button (which the viewer expects to see) and it works exactly as requested. And it mingles with the other buttons on the page. Without complexity.

    I did remove the "type=cancel" which apparently was useless. So even less code. :)

    0 讨论(0)
提交回复
热议问题